代码之家  ›  专栏  ›  技术社区  ›  RonanC

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

  •  0
  • RonanC  · 技术社区  · 6 年前

    https://www.youtube.com/watch?v=8S4EdPJevlA&t=33s 这正是我想要它做的。不过,我已经设置了它,以便搜索在列A ie(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 回复  |  直到 6 年前
        1
  •  0
  •   user10862412 user10862412    6 年前

    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