使用VBA将通用字典轻松打印到Excel工作表

使用VBA将通用字典轻松打印到Excel工作表,vba,excel,dictionary,Vba,Excel,Dictionary,我正在处理非常复杂的数据。正因为如此,我编写了一个非常好的函数,将数据打印到调试区域,即Excel中VBA上使用Ctrl+G可以访问的imediate窗口。我需要一个类似的函数将这些通用数据(包含数字、字符串、字典和数组)打印到工作表中 'call using: Call PrintDict(data) ' Where data can be a number, a string, a dictionary or an Array, ' with any of these inside. S

我正在处理非常复杂的数据。正因为如此,我编写了一个非常好的函数,将数据打印到调试区域,即Excel中VBA上使用Ctrl+G可以访问的imediate窗口。我需要一个类似的函数将这些通用数据(包含数字、字符串、字典和数组)打印到工作表中

'call using: Call PrintDict(data)
' Where data can be a number, a string, a dictionary or an Array, 
' with any of these inside.

Sub PrintDict(ByVal dicttoprint As Variant, Optional indent As Integer = 0, Optional wasdict As Boolean = False)
    Dim i As Long
    Dim j As Long
    Dim indentStr As String
    indentStr = ""
    i = 0

    Do While i < indent
        indentStr = indentStr + " "
        i = i + 1
    Loop

    Dim key

    If (TypeName(dicttoprint) = "Dictionary") Then
        If (wasdict = True) Then
            Debug.Print vbNewLine;
        End If
        For Each key In dicttoprint.Keys:
            Debug.Print indentStr & key & " ";
            Call PrintDict(dicttoprint.Item(key), indent + 2, True)
        Next
    ElseIf (TypeName(dicttoprint) = "Variant()") Then
        If (wasdict = True) Then
            Debug.Print vbNewLine;
        End If
        For j = LBound(dicttoprint) To UBound(dicttoprint)
            Call PrintDict(dicttoprint(j), indent + 2)
        Next j
    Else
        Debug.Print indentStr & dicttoprint & " "
    End If
End Sub
好的,我想现在这个输出解决了一些角落的情况。有没有关于如何实施的想法

我正在考虑让函数能够传递X,Y参数,这些参数是可选的,并返回最后一个Y。当处理文本时,光标自然向下移动,我不知道如何通过工作表中的递归来实现这一点

'call using: Call PrintDict(data)
' Where data can be a number, a string, a dictionary or an Array, 
' with any of these inside.

Sub PrintDict(ByVal dicttoprint As Variant, Optional indent As Integer = 0, Optional wasdict As Boolean = False)
    Dim i As Long
    Dim j As Long
    Dim indentStr As String
    indentStr = ""
    i = 0

    Do While i < indent
        indentStr = indentStr + " "
        i = i + 1
    Loop

    Dim key

    If (TypeName(dicttoprint) = "Dictionary") Then
        If (wasdict = True) Then
            Debug.Print vbNewLine;
        End If
        For Each key In dicttoprint.Keys:
            Debug.Print indentStr & key & " ";
            Call PrintDict(dicttoprint.Item(key), indent + 2, True)
        Next
    ElseIf (TypeName(dicttoprint) = "Variant()") Then
        If (wasdict = True) Then
            Debug.Print vbNewLine;
        End If
        For j = LBound(dicttoprint) To UBound(dicttoprint)
            Call PrintDict(dicttoprint(j), indent + 2)
        Next j
    Else
        Debug.Print indentStr & dicttoprint & " "
    End If
End Sub
编辑2:

好的,这是伪代码的想法-几乎是VBA,但我不知道如何使这项工作

Function PrintToWS(ByVal data As Variant, _
    Optional rowi As Integer = 0, _
    Optional coli As Integer = 0) As Integer

    If (TypeName(data) = "Dictionary") Then
        For Each key In data.Keys:
            Cells(rowi, coli).Value = key
            coli = coli + PrintToWS(data.Item(key), rowi+1, coli)
        Next
    ElseIf (TypeName(data) = "Variant()") Then
        For j = LBound(data) To UBound(data)
            coli = coli + PrintToWS(data(j), rowi+1, coli)
        Next j           
    Else
        Cells(rowi, coli).Value = data
        coli = coli + 1
    End If

    PrintToWS = coli
End Function
编辑2:


已解决。代码如下:

'usage: PrintToWS(yourdata)
' Optional parameters are to be used internally by the function, 
'leave optional parameters blank.

Function PrintToWS(ByVal data As Variant, _
    Optional rowi As Integer = 1, _
    Optional coli As Integer = 1, _
    Optional wasdict As Integer = 0) As Integer

    Dim key
    Dim j As Integer

    If (TypeName(data) = "Dictionary") Then
        For Each key In data.Keys:
            Cells(rowi + wasdict, coli).Value = key
            rowi = PrintToWS(data.Item(key), rowi + wasdict, coli + 1, 1)
            wasdict = 0
        Next
    ElseIf (TypeName(data) = "Variant()") Then
        For j = LBound(data) To UBound(data)
            rowi = PrintToWS(data(j), rowi, coli + 1)
        Next j
    Else
        Cells(rowi, coli).Value = data
        rowi = rowi + 1
    End If

    PrintToWS = rowi
End Function

这是问题的一部分,我不习惯使用工作表,我不知道哪种输出最好。酷,我共享代码,获得否决票,WTFH?我否决了,因为不清楚您要的输出。由于您现在已共享,我删除了DV。单元格(0,0)不存在。A1是细胞(1,1),你比你想象的要近。不要逐行打印文本,而是逐行构建数组。然后通过将数组指定给一个范围将其输出到工作表。更多信息,请参阅和。您可能可以重用大部分工作代码。然后用结果回答你自己的问题!我知道这很古老,但你也有一个代码被剪断,以便在口述中阅读吗?似乎有点难,因为你不知道有多少个级别are@dv3我不明白你在听写中阅读的意思。我有一个代码可以在Excel控制台中打印为文本-是的,Excel有一个控制台。