在VBA中迭代键并将其打印到单元格中

huangapple go评论66阅读模式
英文:

Iterate over keys in VBA and print them in cells

问题

我想遍历字典的键并将它们放入单元格。

我尝试过这样做:

Dim dic1 As Object
Set dic1 = CreateObject("Scripting.Dictionary")
dic1.Add "name1", "value1"
dic1.Add "name2", "value2"
For i = 0 To Len(dic1)
    Cells(1, dic1()(i)) = dic1()(i).Key
Next

我添加了括号以避免错误,但仍然出现“参数个数不正确或无效的属性分配”。

为了回应一条评论,我想避免使用“ForEach”。

编辑:根据此链接,我尝试:

Dim cell1 As String
cell1 = dic1.Items(1)

它不起作用。我得到“未定义属性放置过程,且属性获取过程未返回对象”。这是不是意味着它不是一个字典?

英文:

I would like to iterate through a dictionary's keys and put them in cells.

I tried that:

Dim dic1 As Object
Set dic1 = CreateObject("Scripting.Dictionary")
dic1.Add "name1", "value1"
dic1.Add "name2", "value2"
For i = 0 To Len(dic1)
    Cells(1, dic1()(i)) = dic1()(i).Key
Next

I added the parentheses to avoid an error but I still get Wrong number of arguments or invalid property assignment.

To answer a comment, I would like to avoid For Each.

Edit: following this link, I tried:

Set cell1 As String
cell1 = dic1.Items(1)

It didn't work. I got Property let procedure not defined and property get procedure did not return an object. Does it mean that it's not a dictionary?

答案1

得分: 3

遍历字典元素(早期绑定 vs 晚期绑定)

  • 如果要遍历字典的索引,应使用早期绑定,即创建对Microsoft Scripting Runtime库的引用(TestEarly)。
  • 在没有引用的情况下执行此操作效率低(TestLate)。

<!-- 语言: lang-vb -->

选项 显式


子 进行测试提前()
    
    常量 第一行 作为 长 = 2
    
    &#39; 早期绑定。需要对库进行引用:
    &#39; VBE-&gt;工具-&gt;引用-&gt;Microsoft Scripting Runtime
    Dim 字典 作为 Scripting.Dictionary: 设置 字典 = 新建 Scripting.Dictionary
    
    字典.添加 "name1", "value1"
    字典.添加 "name2", "value2"
    
    &#39; 从 'A2'(键)和 'B2'(项)开始写入。
    
    Dim i 作为 长
    
    对于 i 从 0 到 字典.数目 - 1
        单元格(i + 第一行, "A") = 字典.键(i)
        单元格(i + 第一行, "B") = 字典.项(i)
    下一个 i

    弹出消息框 "找到 " &amp; i &amp; " 项。", vbInformation

结束 子


子 进行测试晚期()

    常量 第一行 作为 长 = 2

    &#39; 晚期绑定。无需库引用
    Dim 字典 作为 对象: 设置 字典 = 创建对象("Scripting.Dictionary")
    
    字典.添加 "name1", "value1"
    字典.添加 "name2", "value2"
    
    &#39; 从 'A2'(键)和 'B2'(项)开始写入。
    
    Dim i 作为 长
    
    对于 i 从 0 到 字典.数目 - 1
        单元格(i + 第一行, "A") = 字典.键()(i)
        单元格(i + 第一行, "B") = 字典.项()(i)
    下一个 i

    弹出消息框 "找到 " &amp; i &amp; " 项。", vbInformation

结束 子


子 进行测试晚期对每个()

    常量 第一行 作为 长 = 2

    &#39; 晚期绑定。无需库引用
    Dim 字典 作为 对象: 设置 字典 = 创建对象("Scripting.Dictionary")
    
    字典.添加 "name1", "value1"
    字典.添加 "name2", "value2"
    
    &#39; 从 'A2'(键)和 'B2'(项)开始写入。
    
    Dim i 作为 长: i = 第一行
    
    Dim 键
    
    对于 每个 键 在 字典.键
        单元格(i, "A") = 键
        单元格(i, "B") = 字典(键)
        i = i + 1
    下一个 键

    弹出消息框 "找到 " &amp; i - 第一行 &amp; " 项。", vbInformation

结束 子
英文:

Looping Over Elements of a Dictionary (Early vs Late Binding)

  • If you want to loop over the indexes of a dictionary, you will want to use early binding i.e. create a reference to the Microsoft Scripting Runtime library (TestEarly).
  • Doing it without the reference is inefficient (TestLate).

<!-- language: lang-vb -->

Option Explicit


Sub TestEarly()
    
    Const FIRST_ROW As Long = 2
    
    &#39; Early Binding. Needs a reference to the library:
    &#39; VBE-&gt;Tools-&gt;References-&gt;Microsoft Scripting Runtime
    Dim dict As Scripting.Dictionary: Set dict = New Scripting.Dictionary
    
    dict.Add &quot;name1&quot;, &quot;value1&quot;
    dict.Add &quot;name2&quot;, &quot;value2&quot;
    
    &#39; Start writing at &#39;A2&#39; (keys) and &#39;B2&#39; (items).
    
    Dim i As Long
    
    For i = 0 To dict.Count - 1
        Cells(i + FIRST_ROW, &quot;A&quot;) = dict.Keys(i)
        Cells(i + FIRST_ROW, &quot;B&quot;) = dict.Items(i)
    Next i

    MsgBox &quot;Found &quot; &amp; i &amp; &quot; item(s).&quot;, vbInformation

End Sub


Sub TestLate()

    Const FIRST_ROW As Long = 2

    &#39; Late Binding. No library reference needed
    Dim dict As Object: Set dict = CreateObject(&quot;Scripting.Dictionary&quot;)
    
    dict.Add &quot;name1&quot;, &quot;value1&quot;
    dict.Add &quot;name2&quot;, &quot;value2&quot;
    
    &#39; Start writing at &#39;A2&#39; (keys) and &#39;B2&#39; (items).
    
    Dim i As Long
    
    For i = 0 To dict.Count - 1
        Cells(i + FIRST_ROW, &quot;A&quot;) = dict.Keys()(i)
        Cells(i + FIRST_ROW, &quot;B&quot;) = dict.Items()(i)
    Next i

    MsgBox &quot;Found &quot; &amp; i &amp; &quot; item(s).&quot;, vbInformation

End Sub


Sub TestLateForEach()

    Const FIRST_ROW As Long = 2

    &#39; Late Binding. No library reference needed
    Dim dict As Object: Set dict = CreateObject(&quot;Scripting.Dictionary&quot;)
    
    dict.Add &quot;name1&quot;, &quot;value1&quot;
    dict.Add &quot;name2&quot;, &quot;value2&quot;
    
    &#39; Start writing at &#39;A2&#39; (keys) and &#39;B2&#39; (items).
    
    Dim i As Long: i = FIRST_ROW
    
    Dim Key
    
    For Each Key In dict.Keys
        Cells(i, &quot;A&quot;) = Key
        Cells(i, &quot;B&quot;) = dict(Key)
        i = i + 1
    Next Key

    MsgBox &quot;Found &quot; &amp; i - FIRST_ROW &amp; &quot; item(s).&quot;, vbInformation

End Sub

答案2

得分: 1

尝试这个:

Dim dic1 As Object
Set dic1 = CreateObject("Scripting.Dictionary")
dic1.Add "name1", "value1"
dic1.Add "name2", "value2"

Dim i As Long
Dim keys As Variant
keys = dic1.Keys

For i = LBound(keys) To UBound(keys)
    Cells(1, i + 1) = keys(i)
Next i
英文:

try this

Dim dic1 As Object
Set dic1 = CreateObject(&quot;Scripting.Dictionary&quot;)
dic1.Add &quot;name1&quot;, &quot;value1&quot;
dic1.Add &quot;name2&quot;, &quot;value2&quot;

Dim i As Long
Dim keys As Variant
keys = dic1.Keys

For i = LBound(keys) To UBound(keys)
    Cells(1, i + 1) = keys(i)
Next i

答案3

得分: 1

If your dictionary has less than 65,536 elements, there is no need to iterate:

Option Explicit
Sub foo()

Dim dic1 As Object
Set dic1 = CreateObject("Scripting.Dictionary")
dic1.Add "name1", "value1"
dic1.Add "name2", "value2"

Range(Cells(1, 1), Cells(dic1.Count, 1)) = WorksheetFunction.Transpose(dic1.Keys)

End Sub

The limit is due to the misbehavior of WorksheetFunction.Transpose when there are more than that number of elements. If you exceed that limit, merely create a 2D variant array by looping.

If you want the output in a single row, omit the WorksheetFunction.Transpose (but you will be limited to the number of columns in Excel.

英文:

If your dictionary has less than 65,536 elements, there is no need to iterate:

Option Explicit
Sub foo()

Dim dic1 As Object
Set dic1 = CreateObject(&quot;Scripting.Dictionary&quot;)
dic1.Add &quot;name1&quot;, &quot;value1&quot;
dic1.Add &quot;name2&quot;, &quot;value2&quot;

Range(Cells(1, 1), Cells(dic1.Count, 1)) = WorksheetFunction.Transpose(dic1.Keys)

End Sub

The limit is due to the misbehaviour of WorksheetFunction.Transpose when there are more than that number of elements. If you exceed that limit, merely create a 2D variant array by looping.

If you want the output in a single row, omit the WorksheetFunction.Transpose (but you will be limited to the number of columns in Excel.

huangapple
  • 本文由 发表于 2023年4月17日 09:02:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76031080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定