将VBA字典打印到文本文件中

时间:2018-03-14 21:29:16

标签: vba excel-vba dictionary text excel

有没有办法将字典打印成文字?我有一个包含100多个键的字典,如果可能的话,我想将它打印到制表符分隔的文本文件中。这似乎很简单,但我无法弄清楚 - 即。我是新的VBA用户。

'Defining text file variable
Dim FilePath As String

'Text file path
 FilePath = path & "\OrderStatus.txt"

'Open the text file
Open FilePath For Output As #1
'OrderStatus is the dictionary
Write #1, OrderStatus

Close #1

2 个答案:

答案 0 :(得分:3)

'Defining text file variable
Dim FilePath As String

'Text file path
 FilePath = path & "\OrderStatus.txt"

Dim key As Variant
'Open the text file
Open FilePath For Output As #1
With OrderStatus
    For Each key In .keys
        Write #1, key & “,” & .Item(key)
    Next
End With
Close #1

答案 1 :(得分:3)

考虑:

Sub Webster()
    Dim dict As Object
    Set dict = CreateObject("Scripting.Dictionary")

    dict.Add "winken", 1
    dict.Add "blinken", 2
    dict.Add "nod", 3

    Close #1
    Open "C:\TestFolder\TestFile.txt" For Output As #1

    For Each ky In dict.keys
        Print #1, ky & " " & dict(ky)
    Next ky

    Close #1
End Sub

enter image description here

(如果您愿意,可以用标签替换空格。)