如何在执行时设置动态数组大小?

时间:2019-05-24 20:12:46

标签: excel vba

我要从数组中删除重复的字符串,并且我很确定我需要使用Redim语句来动态更改数组的大小,但是我无法使其正常工作。

这是我尝试过的:

Dim temp() as String
Dim vetOrg as Variant

vetOrg = Array(contRows)
ReDim vetOrg(1 To contRows)


'populates the array
For i = 1 To contRows
    vetOrg(i) = wsDeals.Cells(i + 1, 2).Value
Next i


 j = 1
    For i = 1 To contRows
        ReDim temp(1 To j)
        If vetOrg(i) <> vetOrg(i + 1) Then
            temp(j = j + 1) = vetOrg(i)
        End If
        temp(j = j + 1) = vetOrg(contRows - 1)
    Next i

1 个答案:

答案 0 :(得分:2)

使用Scripting.Dictionary,您无需担心调整大小或搜索重复值;字典键本质上是唯一的:

Dim vetOrg As Scripting.Dictionary
Set vetOrg = New Scripting.Dictionary

For i = 1 To contRows
    vetOrg(wsDeals.Cells(i + 1, 2).Value) = i ' value is bogus, it's the key we want.
Next i

'done. vetOrg.Keys has the unique values.