Skip to content

Instantly share code, notes, and snippets.

@blcarlson01
Last active February 10, 2023 10:46
Show Gist options
  • Save blcarlson01/d270fdb4bdf912563851373c485e314e to your computer and use it in GitHub Desktop.
Save blcarlson01/d270fdb4bdf912563851373c485e314e to your computer and use it in GitHub Desktop.
This version uses an external JSON Parser
Sub JSON_Transform_Pivot_Chart()
'Declare variables
Dim wsSource As Worksheet
Dim wsDest As Worksheet
Dim rngSource As Range
Dim rngDest As Range
Dim jsonText As String
Dim jsonObject As Object
Dim dict As Object
Dim key As Variant
Dim i As Integer
Dim j As Integer
Dim pivotFields As PivotFields
Dim pivotField As PivotField
Dim pivotTable As PivotTable
'Set worksheet variables
Set wsSource = ThisWorkbook.Sheets("Sheet1")
Set wsDest = ThisWorkbook.Sheets("Sheet2")
'Set source range and clear destination range
Set rngSource = wsSource.Range("A2", wsSource.Range("A" & wsSource.Rows.Count).End(xlUp))
wsDest.Range("A2:Z500").Clear
'Iterate through source range and extract values from JSON
For i = 1 To rngSource.Rows.Count
'Get JSON text
jsonText = rngSource.Cells(i, 1).Value
'Parse JSON text
Set jsonObject = JsonConverter.ParseJson(jsonText)
'Iterate through JSON object and extract values
For Each dict In jsonObject
j = j + 1
wsDest.Cells(j, 1).Value = dict("Key1")
wsDest.Cells(j, 2).Value = dict("Key2")
wsDest.Cells(j, 3).Value = dict("Key3")
'Add additional lines for each key to extract
Next dict
Next i
'Create pivot table
Set rngDest = wsDest.Range("A1").CurrentRegion
Set pivotTable = wsDest.PivotTableWizard(rngDest, "A2:C" & j)
'Format pivot table
With pivotTable
.TableRange2.FormatConditions.AddIconSetCondition
.TableRange2.FormatConditions(1).ShowIconOnly = True
.ColumnGrand = False
.RowGrand = False
End With
'Create pivot chart
Set pivotFields = pivotTable.PivotFields
For Each pivotField In pivotFields
If pivotField.Orientation = xlDataField Then
pivotField.Function = xlCount
pivotField.NumberFormat = "#,##0"
Exit For
End If
Next pivotField
wsDest.Shapes.AddChart2(251, xlBarClustered, 0, 0, pivotTable.TableRange2.Width, pivotTable.TableRange2.Height).Select
End Sub
##
To turn the VBA code into an Excel plugin, you need to follow these steps:
Open Excel and create a new Workbook.
Go to the Developer tab in the ribbon, and click on "Visual Basic" to open the VBA editor.
In the VBA editor, go to File > Import File and import the VBA code that I provided.
In the VBA editor, go to File > Save As and save the workbook as a Macro-Enabled Workbook (.xlsm) file.
Go back to the Excel Workbook and go to the Developer tab in the ribbon.
In the Code group, click on "Macro Security" and select "Trust Access to the VBA Project Object Model."
Go back to the VBA editor, right-click on the module that contains the code and select "Export File."
Save the module as an Excel add-in file with a .xlam extension.
Close the VBA editor and the Excel Workbook.
Go to File > Options > Add-ins and click on "Go" next to "Excel Add-ins."
In the Add-Ins dialog box, click on "Browse" and select the .xlam file that you just saved.
Click on "OK" to close the Add-Ins dialog box.
Now you have successfully created an Excel plugin from the VBA code. You can access the plugin from the Developer tab in the ribbon, and it will be available for use in all Excel Workbooks.
To add a reference to a JSON library in an Excel project, follow these steps:
Open the VBA editor in Excel (press Alt + F11).
In the VBA editor, go to Tools > References.
Scroll down the list of references and select a JSON library. There are several options to choose from, such as "Microsoft Scripting Runtime" or "JSON.NET".
Click on OK to add the reference.
Save your Excel Workbook.
Now the JSON library is referenced in your Excel project, and the JsonConverter object can be used to parse the JSON data.
Note: If you do not see a JSON library in the list of references, you may need to download and install one before you can add it as a reference in your Excel project.
##
Is this the same Json parse that the Excel Data Transform uses?
The JSON parsing used in the Excel Data Transform is a built-in feature of Excel, and it may use a different parsing library than the one referred to in the VBA code. The JSON parsing library referred to in the VBA code is a third-party library that provides additional functionality beyond what is available in the built-in JSON parsing feature of Excel.
The built-in JSON parsing feature in Excel can be used to convert simple JSON data into an Excel table, but it may not be able to handle more complex JSON data structures. In these cases, a third-party library like the one referred to in the VBA code can be used to parse the JSON data and extract the values needed for the pivot table and chart.+
##
Here is a version of the code that uses the built-in JSON parsing feature of Excel:
Sub TransformJsonColumn()
Dim jsonString As String
Dim jsonObject As Object
Dim newSheet As Worksheet
Dim pivotRange As Range
Dim pivotTable As PivotTable
Dim pivotChart As Chart
' Get the JSON string from the first cell in column A
jsonString = Range("A1").Value
' Parse the JSON string into a JSON object
Set jsonObject = JsonConverter.ParseJson(jsonString)
' Create a new worksheet to store the transformed data
Set newSheet = ThisWorkbook.Sheets.Add
' Copy the values from the JSON object to the new worksheet
newSheet.Range("A1").Resize(jsonObject.Count, 2).Value = _
Application.Transpose(jsonObject.keys)
newSheet.Range("B1").Resize(jsonObject.Count, 2).Value = _
Application.Transpose(jsonObject.items)
' Create a pivot table from the transformed data
Set pivotRange = newSheet.Range("A1").CurrentRegion
Set pivotTable = newSheet.PivotTableWizard( _
pivotRange, "Value", "Key", "Value")
' Create a bar chart from the pivot table
Set pivotChart = newSheet.Shapes.AddChart2( _
XlChartType:=xlBarClustered, _
pivotTable:=pivotTable).Chart
End Sub
This code uses the JsonConverter.ParseJson method to parse the JSON string into a JSON object, which can then be used to extract the values and keys. The values and keys are then copied to a new worksheet, where a pivot table and chart are created from the data.
Transform JSON Column into Pivot Table and Chart
This Excel macro allows you to take a column of JSON data, transform the column into a pivot table and chart, and display the result in a new sheet. Here is how to use the macro:
Prepare your data: Make sure that you have a column of JSON data in your Excel workbook. The first cell in the column should contain the JSON data that you want to transform.
Run the macro: Open the VBA editor in Excel by pressing Alt + F11, and then run the macro by pressing the Run button or by pressing F5.
View the results: The macro will create a new sheet in your workbook, which will contain the pivot table and chart based on the transformed JSON data. You can interact with the pivot table and chart just like you would with any other pivot table and chart in Excel.
Note: If the macro encounters any errors while running, it will display an error message. Make sure that the JSON data in your column is well-formed and meets the requirements for the built-in JSON parsing feature of Excel.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment