希望在此语句中添加150个值以进行搜索::: If Cell.Value =" 5"然后

时间:2016-11-16 03:43:31

标签: excel-vba vba excel

我需要根据可在列J中找到的值列表来收集大量数据。对于大约150个限定值,我希望将整行复制到工作簿中的另一个工作表中。 下面的内容让我接近但不确定如何添加150左右的值来查找:

 For Each Cell In Sheets(1).Range("J:J")`enter code here`
If Cell.Value = "ABCDEFG" Then
    matchRow = Cell.Row
    Rows(matchRow & ":" & matchRow).Select
    Selection.Copy

    Sheets("Sheet2").Select
    ActiveSheet.Rows(matchRow).Select
    ActiveSheet.Paste
    Sheets("Sheet1").Select
End If
Next

1 个答案:

答案 0 :(得分:1)

使用Match()

的示例
Sub Tester()

    Dim c As Range, rngTest As Range, rngList As Range

    Set rngList = ThisWorkbook.Sheets("list").Range("A1:A150")

    Set rngTest = Application.Intersect(Sheets(1).Range("J:J"), Sheets(1).UsedRange)

    If Not rngTest Is Nothing Then        
       For Each c In rngTest
            'Does the value in c match any of the values in the
            '   list on sheet "list" range A1:A150 ?
            'If Yes (Match does not return an error) then copy that row
            If Not IsError(Application.Match(c.Value, rngList, 0)) Then
                c.EntireRow.Copy Sheets("Sheet2").Cells(c.Row, 1)
            End If
        Next
    End If

End Sub