英文:
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
' 早期绑定。需要对库进行引用:
' VBE->工具->引用->Microsoft Scripting Runtime
Dim 字典 作为 Scripting.Dictionary: 设置 字典 = 新建 Scripting.Dictionary
字典.添加 "name1", "value1"
字典.添加 "name2", "value2"
' 从 'A2'(键)和 'B2'(项)开始写入。
Dim i 作为 长
对于 i 从 0 到 字典.数目 - 1
单元格(i + 第一行, "A") = 字典.键(i)
单元格(i + 第一行, "B") = 字典.项(i)
下一个 i
弹出消息框 "找到 " & i & " 项。", vbInformation
结束 子
子 进行测试晚期()
常量 第一行 作为 长 = 2
' 晚期绑定。无需库引用
Dim 字典 作为 对象: 设置 字典 = 创建对象("Scripting.Dictionary")
字典.添加 "name1", "value1"
字典.添加 "name2", "value2"
' 从 'A2'(键)和 'B2'(项)开始写入。
Dim i 作为 长
对于 i 从 0 到 字典.数目 - 1
单元格(i + 第一行, "A") = 字典.键()(i)
单元格(i + 第一行, "B") = 字典.项()(i)
下一个 i
弹出消息框 "找到 " & i & " 项。", vbInformation
结束 子
子 进行测试晚期对每个()
常量 第一行 作为 长 = 2
' 晚期绑定。无需库引用
Dim 字典 作为 对象: 设置 字典 = 创建对象("Scripting.Dictionary")
字典.添加 "name1", "value1"
字典.添加 "name2", "value2"
' 从 'A2'(键)和 'B2'(项)开始写入。
Dim i 作为 长: i = 第一行
Dim 键
对于 每个 键 在 字典.键
单元格(i, "A") = 键
单元格(i, "B") = 字典(键)
i = i + 1
下一个 键
弹出消息框 "找到 " & i - 第一行 & " 项。", 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
' Early Binding. Needs a reference to the library:
' VBE->Tools->References->Microsoft Scripting Runtime
Dim dict As Scripting.Dictionary: Set dict = New Scripting.Dictionary
dict.Add "name1", "value1"
dict.Add "name2", "value2"
' Start writing at 'A2' (keys) and 'B2' (items).
Dim i As Long
For i = 0 To dict.Count - 1
Cells(i + FIRST_ROW, "A") = dict.Keys(i)
Cells(i + FIRST_ROW, "B") = dict.Items(i)
Next i
MsgBox "Found " & i & " item(s).", vbInformation
End Sub
Sub TestLate()
Const FIRST_ROW As Long = 2
' Late Binding. No library reference needed
Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
dict.Add "name1", "value1"
dict.Add "name2", "value2"
' Start writing at 'A2' (keys) and 'B2' (items).
Dim i As Long
For i = 0 To dict.Count - 1
Cells(i + FIRST_ROW, "A") = dict.Keys()(i)
Cells(i + FIRST_ROW, "B") = dict.Items()(i)
Next i
MsgBox "Found " & i & " item(s).", vbInformation
End Sub
Sub TestLateForEach()
Const FIRST_ROW As Long = 2
' Late Binding. No library reference needed
Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
dict.Add "name1", "value1"
dict.Add "name2", "value2"
' Start writing at 'A2' (keys) and 'B2' (items).
Dim i As Long: i = FIRST_ROW
Dim Key
For Each Key In dict.Keys
Cells(i, "A") = Key
Cells(i, "B") = dict(Key)
i = i + 1
Next Key
MsgBox "Found " & i - FIRST_ROW & " item(s).", 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("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
答案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("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 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论