匹配函数中的VBA VarType

时间:2017-12-20 23:58:38

标签: excel vba excel-vba

我正在尝试使用excel vba函数Application.Match

匹配函数仅适用于我的第一个数组arrCompare(0)和arrCompare(5)。微软表示传递给函数的变量必须是变体。这个陈述似乎并不完全正确,因为arrCompare(0)& arrCompare(5)是字符串,arrCompare(1)是字符串。我还没有理解为什么当forumla到达剩余的arrCompare(1,2,3,4)变量时,Match函数不起作用。

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

Set sht = ActiveSheet
Set shtTigers = Worksheets("Tigers").Range("A5:A10000")
Set shtElephants = Worksheets("Elephants").Range("A6:A10000")

Sheets("Elephants").Activate

For intRow = 6 To 7

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

'I intend to use this function to compare all 6 array variables but for now am stuck getting 1 at a time to work
varRes = Application.Match(arrCompare(0), shtTigers, 0)

感谢我的脚本错误或导致错误的任何输入。

1 个答案:

答案 0 :(得分:0)

varRes必须是Variant,因为Application.Match可以返回数字或错误值