简体   繁体   English

Range.Find 多个条件,然后将结果复制/粘贴到不同的工作表

[英]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.

相关问题 根据列中的条件复制一定范围的行,然后粘贴到名为条件的其他工作表中 - Copy a range of rows depending on criteria in a column and paste into a different sheet named as the criteria VBA中的多个Range.Find() - Multiple Range.Find() in VBA 如果满足两个范围标准,请复制第三个单元格并将其粘贴到另一张纸上 - If two range criteria are met, copy a third cell and paste it to another sheet Excel VBA在工作表中查找文本,复制范围,粘贴到其他工作表 - Excel vba find text in sheet, copy range, paste to other sheet 从工作表复制范围,然后粘贴到不同列的另一工作表中 - Copy range from a sheet and paste into another sheet in different columns 根据两个条件查找特定行,然后将粘贴范围复制到行中 - Find specific row based on two criteria and then copy paste range into row 有没有一种方法可以复制特定范围内的“浮动图片”,然后使用VBA将其粘贴到另一张纸上? - Is there a way to copy a “floating picture” in a specific range and paste it to different sheet with VBA? 复制指定范围并粘贴到工作表 - Copy a specified range and paste to a sheet Excel VBA复制匹配条件单元格并根据不同工作表中的条件粘贴到特定单元格中 - Excel VBA to copy matching criteria cell and paste in a specific cell based on criteria in a different sheet Range.Find()失败 - Range.Find() fails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM