VBA符合6标准

时间:2018-01-10 00:04:55

标签: excel vba excel-vba

脚本从名为" Tigers"的工作表填充数组。有6个字符串。然后它应该将该数组与标题为" Elephants"并告诉我它是否找到完全匹配。可以在Application.Match方法

中找到麻烦的代码

了解如何正确编写具有多个值的匹配项的任何帮助都将不胜感激。

Sub matchData()

Dim arrCompare(5) As Variant
Dim intRow As Integer
Dim varRes As Variant

Set sht = ActiveSheet
Set shtTigers = Worksheets("Tigers").Range("A2:A100")
Set shtElephants = Worksheets("Elephants").Range("A2:A100")

Sheets("Elephants").Activate

For intRow = 2 To 100

arrCompare(0) = Worksheets("Elephants").Cells(intRow, 1).Value      
arrCompare(1) = Worksheets("Elephants").Cells(intRow, 2).Value      
arrCompare(2) = Worksheets("Elephants").Cells(intRow, 4).Value      
arrCompare(3) = Worksheets("Elephants").Cells(intRow, 5).Value      
arrCompare(4) = Worksheets("Elephants").Cells(intRow, 7).Value      
arrCompare(5) = Worksheets("Elephants").Cells(intRow, 9).Value      

'compare all 6 strings in array against Elephant sheet rows for a match
varRes = Application.Match(arrCompare(), shtTigers, 0)

'also tried
'varRes = Application.Match(((arrCompare(0))*((arrCompare(1))*((arrCompare(2)) * ((arrCompare(3)) * ((arrCompare(4)) * ((arrCompare(5))*((arrCompare(6)),shtTigers, 0)

'messagebox just gives a Error 13 or 2042 for varRes
MsgBox ("varRes = " & varRes)

    Next

End Sub

1 个答案:

答案 0 :(得分:0)

匹配需要单个查找值,但您尝试传递整个数组。不过迭代一个元素:

Dim counter as Integer

    For x = 0 to 5
         If Not IsError(Application.Match(arrCompare(x), shtTigers, 0)) Then
              counter = counter + 1
         End If
    Next x

If counter = 6 Then Debug.Print "Matches found"