Last active
December 7, 2020 22:12
-
-
Save ericoporto/e18bc0ae8432afcd673d634e70c940ae to your computer and use it in GitHub Desktop.
Easy generic print Dictionary to Excel Worksheet using VBA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'call using: Call PrintDict(data) | |
' Where data can be a number, a string, a dictionary or an Array, | |
' with any of these inside. (Ctrl+G for Excel Console) | |
' | |
' The MIT License | |
' | |
' Copyright (c) 2018 Érico Vieira Porto | |
' | |
' Permission is hereby granted, free of charge, to any person obtaining a copy | |
' of this software and associated documentation files (the "Software"), to deal | |
' in the Software without restriction, including without limitation the rights | |
' to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
' copies of the Software, and to permit persons to whom the Software is | |
' furnished to do so, subject to the following conditions: | |
' | |
' The above copyright notice and this permission notice shall be included in | |
' all copies or substantial portions of the Software. | |
' | |
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
' OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
' THE SOFTWARE. | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'usage: PrintToWS(yourdata) | |
' Optional parameters are to be used internally by the function, | |
'leave optional parameters blank. | |
' | |
' The MIT License | |
' | |
' Copyright (c) 2018 Érico Vieira Porto | |
' | |
' Permission is hereby granted, free of charge, to any person obtaining a copy | |
' of this software and associated documentation files (the "Software"), to deal | |
' in the Software without restriction, including without limitation the rights | |
' to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
' copies of the Software, and to permit persons to whom the Software is | |
' furnished to do so, subject to the following conditions: | |
' | |
' The above copyright notice and this permission notice shall be included in | |
' all copies or substantial portions of the Software. | |
' | |
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
' OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
' THE SOFTWARE. | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Print dict to excel WorkSheet. If you use the Console variant, use
Ctrl+G
for Excel Console.