简体   繁体   English

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

[英]Excel VBA How to append column of value to array?

I would like to search through a column of data and compare the values. 我想搜索一列数据并比较值。 If a value is not in the array, then I want to append the value to the array. 如果值不在数组中,那么我想将该值附加到数组中。 Otherwise, I keep looking through each row. 否则,我将继续浏览每一行。 I'm having a little trouble with the syntax. 我在语法上遇到了一些麻烦。 Can I get some help? 我可以帮忙吗?

When I run this I get an error saying "Invalid Procedure call or argument" on the IsError(Application.Match(cells(i, 4).Value, codeArr, False)) function. 当我运行此程序时,在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

Try using this UDF 尝试使用此UDF

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

and then replace 然后更换

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

with

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

I believe it'll accomplish what you're after. 我相信它将满足您的要求。

EDIT Example input: 编辑示例输入:

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

If Column A had 1,2,3 and 4 in rows 1, 2, 3 and 4, respectively, then codeArr would contain the values (4, 5, 6, 1, 2, 3) if you looped i = 1 to 4 . 如果列A在第1、2、3和4行中分别具有1,2、3和4,则codeArri = 1 to 4循环,则codeArr将包含值( codeArr ) 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM