是什么导致阵列类型不匹配?

时间:2016-03-08 14:29:47

标签: arrays excel vba

我使用for循环创建一个包含有效订单号的数组,然后将其用作另一个查询表的搜索条件。下一个for循环搜索先前创建的数组中的每个订单号,如果该行不在数组中,则删除该行。我想知道为什么我在搜索函数的条件语句行中遇到类型不匹配错误。我尝试将数组声明为变量和具有单独字符串元素的数组。这是修剪过的代码,感谢先进!

Sub VistaArray()
Dim n As Integer, lastrow As Integer, ordern As String, vista() As Variant
'ADDING NEW ELEMENTS TO ORDER NUMBER ARRAY
For n = 2 To lastrow
    i = n - 2
    ReDim Preserve vista(i)
    ordern = Worksheets(Sheet1).Cells(n, 1).Value
    vista(i) = ordern
Next n

'REMOVING LINES FROM SECOND TABLE THAT AREN'T IN THE ARRAY
lastrow = Worksheets(Sheet2).Range("A1").End(xlDown).Row
For n = 2 To lastrow
    ordn = ActiveSheet.Cells(n, 1).Value
    If IsInArray(ordn, vista) Then
    Else
        '...REMOVE LINE FROM QUERY TABLE...
    End If
Next n
End Sub

Function IsInArray(ordn As String, vista As Variant) As Boolean
IsInArray = (UBound(Filter(ordn, vista)) > -1) '***ERROR OCCURS ON THIS LINE***
End Function

1 个答案:

答案 0 :(得分:0)

函数Filter期望第一个参数的数组和第二个参数的字符串。你有逆转。以下应该有效:

int num = 0;
do {
    num = scanner.nextInt();
} while(num > 21 || num < 1);