【问题标题】:Declaring Arrays Dynamically in VBA在 VBA 中动态声明数组
【发布时间】:2019-12-16 19:42:45
【问题描述】:

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

样本数据:

使用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.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)) As Variant 行给了我编译错误

需要常量表达式

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

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

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

【问题讨论】:

  • 为什么不使用ReDim
  • 不能动态声明变量。 Dim 语句不可执行。这就是为什么这听起来像是 X-Y 问题。
  • 这是你正在尝试的吗? 1. Dim varkey As Variant 2. Dim n as Long: n=Val(dict.Item(varKey)) 3. Redim varkey(n)
  • @SiddharthRout .. 这就是我想要做的。但这里 varkey 也是一个变量。鉴于 BigBen 的回答,也许这是不可能的
  • 集合字典可能是您正在寻找的。数组字典也是可以的,但是加入字典后修改数组效率不高。

标签: arrays excel vba dictionary


【解决方案1】:

我不确定手头的任务到底是什么,但如果我了解您的 cmets,这是一个 X-Y 问题。

Dim 语句 - declarative statements - 不可执行。这与类型无关(StringLongVariant 数组,等等。)您的问题标题在这方面可能有点误导,因为基本上您似乎在尝试动态声明 变量 - 它们是数组 的事实是巧合。

您可以通过ReDimming 根据您的字典中的计数创建一个数组来避免编译错误,但您无法提供变量的动态列表。

【讨论】:

    【解决方案2】:

    正如 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
    

    【讨论】:

    • 有什么方法可以打电话给Debug.Print Map(pdf).Count 吗?它会起作用吗?如果我只想使用 pdf .. 我希望能够直接调用它而无需遍历键.. 就像 pdf(1) 用于第一个 pdf 文件或 pdf(ubound(pdf)) 用于最后一个 pdf 文件
    • 酷 .. 他们只是作为一个数组工作,我必须添加使用 Map("pdf") 而不是 pdf ?
    • 谢谢伙计!我什至对解决此问题的方法都失去了希望。
    猜你喜欢
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 2023-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    • 2012-01-11
    相关资源
    最近更新 更多