【问题标题】:Excel vba search in arrayExcel vba在数组中搜索
【发布时间】:2014-06-26 06:26:17
【问题描述】:

我需要在数组中搜索

Sub f()
    Dim myArray As Variant
    myArray = Worksheets("QQ").Range("D:F")

    Dim searchTerm As String
    searchTerm = "927614*"

    'Check if a value exists in the Array
    If UBound(Filter(myArray, searchTerm)) >= 0 And searchTerm <> "" Then
        MsgBox "Your string match value from F column is " & myArray(Application.Match(searchTerm, myArray, False),3)
    Else
        MsgBox ("Search Term could NOT be located in the Array")
    End If
End Sub

但我得到错误类型不匹配。那么如何在数组中查找带 * 的值呢?

【问题讨论】:

  • 您创建了一个 BIDIMENSIONAL 数组,Filter 只接受一维数组。如果你想搜索一个值,你可以使用类似的方法: Worksheets("QQ").Range("D:F").find ("927614*", lookin:=xlValues).row 最终 FindNext ...跨度>
  • 但查找速度非常慢,因为我需要搜索 10000 个搜索项值
  • 一维数组也报错
  • 你是如何制作一维数组的?您还可以对每列 (3) 使用 VLOOKUP,并在 VBA 或下面的答案中阅读后...

标签: arrays excel vba ms-access search


【解决方案1】:

只需遍历数组并使用Like

未经测试的代码:

Dim matchFound As Boolean
matchFound = False
For i = 1 To UBound(myArray, 1)
    For j = 1 To UBound(myArray, 2)
        If myArray(i, j) Like searchTerm Then
            MsgBox "Found match at (" & i & "," & j & ") : " & myArray(i, j)
            matchFound = True
            Exit For
        End If
    Next j
    If matchFound Then Exit For
Next i
If Not matchFound Then MsgBox "No match found."

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-12
    • 2017-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多