[英]Range.Find multiple criteria then copy/paste results to different sheet
I'm trying to use Range.Find
to search in the "Database" sheet to match all the criteria in the "Search" sheet.我正在尝试使用
Range.Find
在“数据库”表中进行搜索,以匹配“搜索”表中的所有条件。 If all the criteria are met, then it copies the entire row (from "Database") and pastes it into the "Results" sheet.如果满足所有条件,则它会复制整行(来自“数据库”)并将其粘贴到“结果”表中。 It keeps looping until all the results that match are found, and copies/pastes the results as a new row for each match.
它一直循环直到找到所有匹配的结果,并将结果复制/粘贴为每个匹配的新行。
For example, this is what "Search" might looks like , where it'd take the criteria from Column I and find all results in "Database" (in no particular order) by comparing it to the construction code (Column D).例如,这就是“搜索”的样子,它会从 I 列中获取条件,并通过将其与构造代码(D 列)进行比较,在“数据库”中找到所有结果(无特定顺序)。 In this case, the 2nd and 3rd row matches all the criteria from "Search", so it'd be copied and pasted to "Results"
在这种情况下,第 2 行和第 3 行匹配“搜索”中的所有条件,因此将被复制并粘贴到“结果”
I'm new to VBA and have looked through many posts to try and put it all together.我是 VBA 的新手,并且已经浏览了许多帖子以尝试将它们放在一起。 Right now my issue is nothing is showing up in "Results" (I'm guessing I haven't written it to search it properly).
现在我的问题是“结果”中没有显示任何内容(我猜我没有写它来正确搜索它)。 Any help would be appreciated, thanks.
任何帮助将不胜感激,谢谢。
Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet
Dim lastRow As Long, j As Long
Set ws1 = Sheets("Database")
Set ws2 = Sheets("Results")
Set ws3 = Sheets("Search")
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
Dim Criteria As Variant
Criteria = ws3.Range("I4:I7").Value
Set searchRng = ws1.Range("D7:D98")
Set rngfindvalue = searchRng.Find(what:=Range("I4"), Lookat:=xlPart)
If Not rngfindvalue Is Nothing Then
rngFirstAddress = rngfindvalue.Row
Do
If rngfindvalue.Value = Criteria(2, 1) And _
rngfindvalue.Value = Criteria(3, 1) And _
rngfindvalue.Value = Criteria(4, 1) Then
lastRow = lastRow + 1
j = rngfindvalue.Row
ws1.Rows(j).EntireRow.Copy ws2.Range("A" & lastRow)
Set rngfindvalue = searchRng.FindNext(rngfindvalue)
End If
Loop Until rngfindvalue Is Nothing Or rngfindvalue.Row = rngFirstAddress
End If
If Not rngfindvalue Is Nothing Then
Application.Goto Reference:=Worksheets("Results").Range("A1")
Else
MsgBox "No results matched your search criteria."
End If
Try the next code, please.请尝试下一个代码。 It should be very fast.
它应该非常快。 It uses arrays and works only in memory.
它使用 arrays 并且仅适用于 memory。 The criteria evaluation result is loaded in an array and dropped in the "Results" worksheet at once.
标准评估结果被加载到一个数组中,并立即放入“结果”工作表中。 The code assumes that the columns header of the "Database" worksheet is on the 6th row:
代码假定“数据库”工作表的列 header 位于第 6 行:
Sub CopyRowsByCriteria()
Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet
Dim lastRow As Long, i As Long, j As Long, k As Long, El As Variant
Dim arrCrit As Variant, arrDB As Variant, arrFin As Variant, boolExclude As Boolean
Set ws1 = Sheets("Database")
Set ws2 = Sheets("Results")
Set ws3 = Sheets("Search")
lastRow = ws1.Cells(Rows.Count, "A").End(xlUp).row
arrCrit = ws3.Range("I4:I7").Value
arrDB = ws1.Range("A6:D" & lastRow).Value 'including head of the table
ReDim arrFin(1 To 4, 1 To UBound(arrDB, 1))
k = k + 1
For i = 1 To 4
arrFin(i, k) = arrDB(1, i) 'fill head of the table
Next i
For i = 2 To UBound(arrDB, 1)
For Each El In arrCrit
If InStr(arrDB(i, 4), El) = 0 Then boolExclude = True: Exit For
Next
If Not boolExclude Then
k = k + 1
For j = 1 To 4
arrFin(j, k) = arrDB(i, j)
Next j
End If
boolExclude = False
Next i
ReDim Preserve arrFin(1 To 4, 1 To k)
'Drop the arrFin data in the Results sheet
ws2.Range("A1").Resize(UBound(arrFin, 2), 4).Value = WorksheetFunction.Transpose(arrFin)
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.