比较VBA中Array的值,如果是重复,则不增加数组或添加值

时间:2016-05-19 20:56:58

标签: arrays vba

我正在尝试循环遍历非连续值的电子表格,并在递增数组维度并将值添加到数组之前,将值读取/比较先前读入数组的其余值。我试着用下面的一个小例子进行演示。

即。

Sub ArrayCompare()
Dim Cntry() As String
ArrayDim = 5 'The array is dimensioned with another counter that is not     pertinent to this question but typically not greater than 5 in 1 dimension
ReDim Cntry(ArrayDim)
Range("C1").Select
Dim Counter As Integer

Counter = 8 'In the real spread sheet the counter is dynamic, ive just put this in as an example

Do Until Counter = 0
    ArrayCounter = 0 'This is used to compare the array values Cntry(C0)

    Do Until ActiveCell.Value <> ""
    If ActiveCell.Value = "" Then
    ActiveCell.Offset(1, 0).Select
    Else: End If
    Loop

        If Active.Value = Cntry(ArrayCounter - 1) Or ActiveCell.Value = Cntry(ArrayCounter - 2) Or ActiveCell.Value = Cntry(ArrayCounter - 3) Or ActiveCell.Value = Cntry(ArrayCounter - 4) Then 'this doesn't work because the array is not dimensioned to this size yet. 
         ActiveCell.Offset(1, 0).Select
         Else
         Cntry(ArrayDim) = ActiveCell.Value
         ArrayDim = ArrayDim + 1
         End If
Counter = Counter - 1
Loop
End Sub

1 个答案:

答案 0 :(得分:0)

使用收藏集,他们更容易使用。这是一个示例代码,您可以在其中循环存储的值集合,并在找到重复项时标记标记。如果您坚持使用它们,可以使用数组完成同样的事情

data table1_2;
set table1_1;
by id;
retain id_temp;
if first.id then 
  id_temp=new_id; 
new_id_fin=id_temp;
run;