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

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

'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 

编辑1:好吧,一直在想,我有一个想法,但不能解决一些angular落的情况下…

预期的输出示例如下:

 key1:____|__________|__________|__________|_________| _________|key1.1:___|_numvalue_|__________|_________| _________|__________|_numvalue_|__________|_________| _________|__________|_arr1Indx1|_numvalue_|_________| _________|__________|_arr1Indx2|_numvalue_|_________| _________|__________|_arr1Indx3|_numvalue_|_________| _________|key1.2:___|_numvalue_|__________|_________| _________|__________|_numvalue_|__________|_________| key2:____|_numvalue_|__________|__________|_________| key3:____|__________|__________|__________|_________| _________|_arr2Indx1|keyA.1:___|_numvalue_|_________| _________|__________|keyA.2:___|_strvalue_|_________| _________|_arr2Indx2|_numvalue_|__________|_________| 

好吧,我认为现在这个输出解决了一些angular落的情况。 有关如何实施它的任何想法?

我在考虑让函数能够传递X,Y参数,这是可选的,并返回最后的Y.当处理文本时,光标自然会下降,我不知道如何通过recursion在一个工作表。

编辑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 

解决了。 代码如下:

 'usage: PritToWS(yourdata) ' Optionar parameters are to be used internally by the function, 'leave 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