Excel VBA如何将数值的列附加到数组?

我想search一列数据并比较这些值。 如果一个值不在数组中,那么我想把值追加到数组中。 否则,我会继续浏览每一行。 我在语法上遇到了一些麻烦。 我能得到一些帮助吗?

当我运行这个时,我得到一个错误说IsError(Application.Match(cells(i,4).Value,codeArr,False))函数的“无效的过程调用或参数”。

For i = 1 To 17381 If IsError(Application.Match(cells(i, 1).Value, codeArr, False)) Then ReDim Preserve codeArr(count) codeArr(count) = cells(i, 1) count = count + 1 End If Next i 

尝试使用这个UDF

 Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean IsInArray = UBound(Filter(arr, stringToBeFound)) > -1 End Function 

然后更换

 If IsError(Application.Match(cells(i, 1).Value, codeArr, False)) Then 

 If Not IsInArray(cells(i, 1).Value, codeArr) Then 

我相信它会完成你以后的事情。

编辑示例input:

 Dim codeArr As Variant codeArr = Array(4, 5, 6) cnt = 4 'Use this instead of Count as Count is a reserved word 

如果列A分别在行1,2,3和4中有1,2,3和4,则如果循环i = 1 to 4 ,则codeArr将包含值( codeArr ) 。