3

我有大字典键,我想将其插入到 excel 表的列中。我通过以下方式完成了:

Dim a, d, i 'Create some variables
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens" 'Add some keys and items.
d.Add "b", "Belgrade"
d.Add "c", "Cairo"

a = d.keys 'Get the keys

For i = 0 To d.Count - 1 'Iterate the array
    ob.Cells(i + 1, 1).Value = a(i)
Next

但它可以在一个声明中完成吗?说obj2.Cells(1,1).Resize(dicP.Count,1)=a我也试过这个,但它只把第一个键放在我定义的范围内Resize

4

1 回答 1

3

像这样?

Sub a()
    Dim d '<~~ Create some variable
    Dim sKeys()

    Set d = CreateObject("Scripting.Dictionary")
    d.Add "a", "Athens" 'Add some keys and items.
    d.Add "b", "Belgrade"
    d.Add "c", "Cairo"

    '~~> Get dictionary Keys
    sKeys = d.keys

    Range("A1").Resize(d.Count) = Application.Transpose(sKeys)
End Sub
于 2012-12-21T08:09:00.240 回答