在工作表数据中搜索多个值

时间:2019-01-07 22:57:10

标签: excel vba

我已经跟随该视频创建了从一张纸到另一张纸的相同搜索功能。 https://www.youtube.com/watch?v=8S4EdPJevlA&t=33s 这几乎正​​是我想要执行的操作。但是,我将其设置为使搜索看起来在列A中,即(x,1)。这样的代码使得它在搜索框“ B3”和(x,1)中使用“ =”。

问题是我想在A列(x,1)行中具有多个标识符,并且某些行将共享标识符。但是,目前只能使用单个标识符。除了能够复制上述某些视频之外,我对VBA几乎一无所知。

从我的有限阅读中,也许我应该尝试实现字符串VBS函数?我不知道

Sub searchMultipleValues()
Dim erow As Long
Dim ws As Worksheet
Dim lastrow As Long
Dim count As Integer

lastrow = Sheets("SRA").Cells(Rows.count, 1).End(xlUp).Row
Sheet1.Range("A14:K150").ClearContents

count = 0

Dim p As Long

p = 14

For x = 2 To lastrow

If Sheets("SRA").Cells(x, 1) = Sheet1.Range("B3") Then
Sheet1.Cells(p, 1) = Sheets("SRA").Cells(x, 1)
Sheet1.Cells(p, 2) = Sheets("SRA").Cells(x, 2)
Sheet1.Cells(p, 3) = Sheets("SRA").Cells(x, 3)
Sheet1.Cells(p, 4) = Sheets("SRA").Cells(x, 4)
Sheet1.Cells(p, 5) = Sheets("SRA").Cells(x, 5)
Sheet1.Cells(p, 6) = Sheets("SRA").Cells(x, 6)
Sheet1.Cells(p, 7) = Sheets("SRA").Cells(x, 7)
Sheet1.Cells(p, 8) = Sheets("SRA").Cells(x, 8)
Sheet1.Cells(p, 9) = Sheets("SRA").Cells(x, 9)
Sheet1.Cells(p, 10) = Sheets("SRA").Cells(x, 10)
Sheet1.Cells(p, 11) = Sheets("SRA").Cells(x, 11)

p = p + 1

count = count + 1

End If
Next x

End Sub

所以最终,我希望能够在A列的行中使用多个搜索词,并让A列的某些行共享并包含相同的可搜索标识符,因为它们可能适用于不同的行

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您可以使用工作表的匹配功能轻松检查多个单元格是否匹配。

For x = 2 To lastrow
    If Not IsError(Application.Match(Sheets("SRA").Cells(x, 1), Sheet1.Range("B3:B9"), 0)) Then

        Sheet1.Cells(p, 1) = Sheets("SRA").Cells(x, 1)
        Sheet1.Cells(p, 2) = Sheets("SRA").Cells(x, 2)
        Sheet1.Cells(p, 3) = Sheets("SRA").Cells(x, 3)
        Sheet1.Cells(p, 4) = Sheets("SRA").Cells(x, 4)
        Sheet1.Cells(p, 5) = Sheets("SRA").Cells(x, 5)
        Sheet1.Cells(p, 6) = Sheets("SRA").Cells(x, 6)
        Sheet1.Cells(p, 7) = Sheets("SRA").Cells(x, 7)
        Sheet1.Cells(p, 8) = Sheets("SRA").Cells(x, 8)
        Sheet1.Cells(p, 9) = Sheets("SRA").Cells(x, 9)
        Sheet1.Cells(p, 10) = Sheets("SRA").Cells(x, 10)
        Sheet1.Cells(p, 11) = Sheets("SRA").Cells(x, 11)

        p = p + 1
        Count = Count + 1

    End If
Next x