VBA查询:使用剪贴板数据进行过滤

我在Excel中使用多个工作表来创build一个正在接受技术培训的候选人数据库。 每次将候选人添加到“数据库”时,他们被分配一个唯一的编号,例如“2015-0001”。 当他们打电话支付押金时,我正在使用电话运营商的数据input表来logging详细信息,并且查找候选人的唯一号码。 然后,我想过滤候选人的主数据库的数量,并粘贴在确认的存款详情。

我的查询是这样的:如何编写从工作表1的单元格复制候选人数据数据的代码,然后使用该数据(不考虑其值)来筛选工作表2?

我是新来的macros,一直在使用“loggingmacros”生成的代码,然后我编辑和学习,因为我去。 所以,如果这看起来非常笨重道歉。 使用logging,filter命令简单地使用我正在使用的示例文本(在这种情况下是2015-0011),而不是在存入input表更改并运行macros时将其replace为修订值。 我是否认为我需要使用string?

提前致谢。 RLC

Sub Confirm_Deposit() ' ' Confirm_Deposit Macro ' ' Sheets("Take Deposit").Select Range("C5").Select Selection.Copy Sheets("CIP Candidates").Select ActiveSheet.Range("$A$6:$AK$2507").AutoFilter Field:=1, Criteria1:= _ "2015-0011" <---------------- ISSUE Sheets("Take Deposit").Select Range("C6:C8").Select Application.CutCopyMode = False Selection.Copy Sheets("CIP Candidates").Select Range("A6").Select Range(Selection, Selection.End(xlDown)).Select Selection.Offset(0, 20).Select Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=True Application.Run _ "'CIP Spreadsheet RLC (with Macros).xlsm'!ThisWorkbook.Clear_Filters" etc. 

过了一段时间,但这会做的伎俩?

  ActiveSheet.Range("$A$6:$AK$2507").AutoFilter Field:=1, Criteria1:= _ Sheets("Take Deposit").Cells(5,3).Value 

要么

 ActiveSheet.Range("$A$6:$AK$2507").AutoFilter Field:=1, Criteria1:= _ Sheets("Take Deposit").Range("C5").Value 

没有必要select和复制值。 您可以引用单元格值。

这是一种不同于“macros”的方法,更多的是使用循环进行简单的单元操作。 这非常灵活。 看看你对这个想法的看法,然后我们可以修改你的具体需求。

我将立即更改的代码部分是select查找值的来源。 在这个例子中,因为我不知道你的具体情况,所以我在上面的例子中看到你正在使用“C5”。

 Sub Confirm_Deposit() Dim source As String Dim target As String Dim lookupVal As String Dim row As Long Dim searchRow As Long source = "Take Deposit" 'In case you have similar projects, you can just replace these lines. target = "CIP Candidates" lastSourceRow = Sheets(source).Range("A" & Rows.Count).End(xlUp).row lastTargetRow = Sheets(target).Range("A" & Rows.Count).End(xlUp).row lastTargetCol = Sheets(target).Cells(1, Columns.Count).End(xlToLeft).Column lookupVal = TextBox1.Text 'Set the lookupVal from whatever source you choose. I like ComboBoxes when I can. For searchRow = 2 To lastSourceRow If Sheets(source).Cells(searchRow, 3).Text = lookupVal Then 'Searching through Source Sheet on Col "C" Exit For End If Next searchRow 'This way, at the end of the search, you have the row number of the original source to be copied, instead of hard coding. For row = 6 To lastTargetRow 'Loop through the Target Sheet If Sheets(target).Cells(row, 3).Text = lookupVal Then 'Compare lookupVal to the Range being looped. For col = 2 To lastTargetCol Sheets(target).Cells(row, 3) = Sheets(source).Cells(searchRow, col) 'Copies contents from Row 5 of source sheet. Next col End If Next row End Sub 

编辑:查找行dynamic,而不是硬编码到第5行