Arrays 在VBA中动态声明数组

Arrays 在VBA中动态声明数组,arrays,excel,vba,dictionary,Arrays,Excel,Vba,Dictionary,我正在尝试动态创建特定长度的数组,以便在更大的过程中使用它们 样本数据: Dim dict As New Scripting.Dictionary For Each cel In Range("B1:B8") I = 1 If Not dict.Exists(cel.Text) Then dict.Add cel.Text, I Else temp = dict(cel.Text) + 1 dict.Remove cel

我正在尝试动态创建特定长度的
数组
,以便在更大的过程中使用它们

样本数据:

Dim dict As New Scripting.Dictionary

For Each cel In Range("B1:B8")
    I = 1
    If Not dict.Exists(cel.Text) Then
        dict.Add cel.Text, I

    Else
        temp = dict(cel.Text) + 1
        dict.Remove cel.Text
        dict.Add cel.Text, temp
    End If
Next cel

For Each varKey In dict.Keys

Debug.Print varKey & ":" & dict.Item(varKey)

Next

下面使用
字典的代码提供了数据中的计数和唯一文件扩展名

代码:

Dim dict As New Scripting.Dictionary

For Each cel In Range("B1:B8")
    I = 1
    If Not dict.Exists(cel.Text) Then
        dict.Add cel.Text, I

    Else
        temp = dict(cel.Text) + 1
        dict.Remove cel.Text
        dict.Add cel.Text, temp
    End If
Next cel

For Each varKey In dict.Keys

Debug.Print varKey & ":" & dict.Item(varKey)

Next
结果:

Dim dict As New Scripting.Dictionary

For Each cel In Range("B1:B8")
    I = 1
    If Not dict.Exists(cel.Text) Then
        dict.Add cel.Text, I

    Else
        temp = dict(cel.Text) + 1
        dict.Remove cel.Text
        dict.Add cel.Text, temp
    End If
Next cel

For Each varKey In dict.Keys

Debug.Print varKey & ":" & dict.Item(varKey)

Next

我尝试创建3个(在本示例中)数组
pdf(4)
xlsx(3)
docm(1)
使用字典中的结果

但是作为变量的Dim varkey(dict.Item(varkey))行
给了我编译错误

需要常量表达式

有办法吗?我在谷歌上搜索实现这一目标的方法,但运气不佳

基本上,我想要的是使用这些不同的扩展名来声明数组。但是这些扩展名会有所不同,所以我需要动态地声明它们。数组应具有与扩展名相同的名称


因此,从工作表或字典中选择名称,并将其声明为指定长度的数组。之后也可以重新定义长度,但主要问题是从变量声明长度。

我不确定手头的任务是什么,但如果我理解您的评论,这是一个X-Y问题

Dim
语句--不可执行。这与类型无关(
String
Long
Variant
array,无论什么)。在这方面,您的问题标题可能有点误导,因为看起来您实际上是在尝试动态声明变量-它们是数组这一事实是巧合的


您可以通过
ReDim
ming基于字典中的计数的数组来避免编译错误,但是您无法生成变量的动态列表

正如BrakNicku评论的那样,一本词典将为您提供您想要的答案

Sub PrintExtensionCount()
    Dim Cell As Range
    Dim Map As New Scripting.Dictionary, subMap As New Scripting.Dictionary

    For Each Cell In Range("B1:B8")
        If Not Map.Exists(Cell.Value) Then Map.Add Cell.Text, New Dictionary

        Set subMap = Map(Cell.Value)
        subMap.Add Cell.Offset(0, -1).Value, vbNullString
    Next

    Dim Key As Variant

    For Each Key In Map
        Set subMap = Map(Key)
        Debug.Print Key; ":"; subMap.Count
    Next

End Sub
结果

我不想把事情搞混,但我喜欢用ArrayList字典

Sub PrintExtensionCount()
    Dim Cell As Range
    Dim Map As New Scripting.Dictionary, list As Object

    For Each Cell In Range("B1:B8")
        If Not Map.Exists(Cell.Value) Then Map.Add Cell.Text, CreateObject("System.Collections.ArrayList")

        Set list = Map(Cell.Value)
        list.Add Cell.Offset(0, -1).Value
    Next

    Dim Key As Variant

    For Each Key In Map
        Set list = Map(Key)
        Debug.Print Key; ":"; list.Count
    Next

End Sub

为什么不使用
ReDim
?您不能动态声明变量<代码>Dim
语句不可执行。这就是为什么这听起来像是一个X-Y问题。这就是你正在尝试的吗?1. <代码>变桨变桨变桨变桨变桨变桨变桨变桨变桨变桨变桨变桨变桨变桨变桨变桨变桨变桨变桨变桨变桨变桨变桨变桨变桨变桨变桨变桨变桨变桨变桨<代码>尺寸n,长度为:n=Val(dict.Item(varKey))
3<代码>雷迪姆·瓦基(n)@SiddharthRout。。这正是我想做的。但这里varkey也是一个变量。也许这是不可能的,因为BigBenDictionary的答案可能就是你想要的。数组字典也是可能的,但在添加到字典后修改这些数组是无效的。有没有办法调用
Debug.Print Map(pdf).Count
?行吗?如果我只想使用pdf。。我希望能够直接调用它,而无需循环键。。如第一个pdf文件的
pdf(1)
,或最后一个pdf文件的
pdf(ubound(pdf))
。。它们只是作为一个数组工作,我必须添加使用
Map(“pdf”)
,而不是
pdf
,你应该看:并阅读谢谢,伙计!我甚至对解决这个问题都失去了希望。