Please can you help me write some VBA code, i want to be able create a table with excel, I want to be able to store the column headers and formulas to use in each column in an easy manner so that excel can create the table on demand, what do you suggest ?
Assistant
Certainly! I suggest using a VBA subroutine to create a table with column headers and formulas. In this example, I'll create a table with three columns and apply a formula to the third column. You can easily customize this code to fit your needs by modifying the headers and formulas arrays.
Press ALT + F11 to open the Visual Basic for Applications (VBA) editor.
Click Insert > Module to insert a new module.
Copy and paste the following code into the module:
User
if you had over 40 formulas which will populate 40 columns and their header names to store what would be the best approach to handle this, would you advise storing these in a dictionary
Assistant
Yes, using a Dictionary object is a great approach to handle a large number of headers and formulas. This will make your code more organized and easier to maintain.
Here's an example of how you can modify the previous code to use a Dictionary:
Follow the same steps to open the VBA editor and insert a new module.
Copy and paste the following code into the module:
Sub CreateTable()
' Declare variables
Dim ws As Worksheet
Dim tbl As ListObject
Dim headerFormulaDict As Object
Dim key As Variant
Dim i As Long
' Create a dictionary to store headers and formulas
Set headerFormulaDict = CreateObject("Scripting.Dictionary")
' Add headers and formulas to the dictionary
headerFormulaDict("Header1") = ""
headerFormulaDict("Header2") = ""
headerFormulaDict("Header3") = "=[@Header1] * [@Header2]"
' Add more headers and formulas as needed
' Set the active worksheet
Set ws = ActiveSheet
' Create a table with specified headers
With ws
.Cells(1, 1).Resize(1, headerFormulaDict.Count).Value = headerFormulaDict.Keys
Set tbl = .ListObjects.Add(xlSrcRange, .Range("A1").Resize(1, headerFormulaDict.Count), , xlYes)
tbl.Name = "MyTable"
End With
' Apply formulas to columns
i = 1
For Each key In headerFormulaDict.Keys
If headerFormulaDict(key) <> "" Then
tbl.ListColumns(i).DataBodyRange.Formula = headerFormulaDict(key)
End If
i = i + 1
Next key
End Sub
User
I want to be able to create different tables can you separate the table creation from the dictionary population
Assistant
Sure! We can separate the table creation from the dictionary population by creating two separate subroutines: one for populating the dictionary and another for creating the table.
User
When i populate the table with the formulas from the dictionary I will want to also have the option to insert these columns to an existing table as well as creating a new one
Assistant
You can modify the CreateTable subroutine to accept an optional parameter . When this parameter is provided, the subroutine will insert columns into the existing table, and if not, it will create a new table.
User
Some of the columns will be grouped into specific sections, I would wish to be able to define a group for some of these columns and depending on what group they are in I wish to put them in different positions in the table via the name of the column in the table that already exists so the column can be placed next to it
Assistant
User
I also wish to store formatting information for each of the column groups, specifically the heading cell colour
Assistant
User
for the column formatting I wish to be able to set the following VBA values for the columns interior
.pattern
.PatternColorindex
.color
.tintandshade
.PatternTintandShade
Assistant
User
Can you store the headerFormulaDict and groupFormattingDict in a separate module from the rest of the code for easy readability ? This would allow non technical users to update the formulas without having to worry about reading the rest of the VBA Code.
Assistant
Sure, you can store the headerFormulaDict and groupFormattingDict in a separate module, making it easier for non-technical users to update the formulas and formatting.
Create a new module (e.g., Module1) for the main code and another module (e.g., Module2) to store the dictionaries.
User
Would you recommend using classes for this code ?
Assistant
Using classes can help make the code more modular and maintainable, especially if you plan to expand the functionality or have multiple table configurations. By defining a custom class, you can encapsulate the logic of creating and updating tables, making it easier to manage and reuse.
Here's an example of how you can refactor the code using classes:
Create a new class module and name it TableBuilder. Add the following code:
' TableBuilder Class
Private pHeaderFormulaDict As Object
Private pGroupFormattingDict As Object
Property Get HeaderFormulaDict() As Object
Set HeaderFormulaDict = pHeaderFormulaDict
End Property
Property Let HeaderFormulaDict(headerFormulaDict As Object)
Set pHeaderFormulaDict = headerFormulaDict
End Property
Property Get GroupFormattingDict() As Object
Set GroupFormattingDict = pGroupFormattingDict
End Property
Property Let GroupFormattingDict(groupFormattingDict As Object)
Set pGroupFormattingDict = groupFormattingDict
End Property
Sub CreateTable(Optional existingTableName As String = "")
' ... (the CreateTable subroutine code remains unchanged)
End Sub
User
can you please write out all of the code
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Sub CreateTable()
' Declare variables
Dim ws As Worksheet
Dim tbl As ListObject
Dim headers() As Variant
Dim formulas() As Variant
Dim lastRow As Long, i As Long
' Define the headers and formulas
headers = Array("Header1", "Header2", "Header3")
formulas = Array("", "", "=[@Header1] * [@Header2]")
' Set the active worksheet
Set ws = ActiveSheet
' Create a table with specified headers
With ws
.Cells(1, 1).Resize(1, UBound(headers) + 1).Value = headers
lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
Set tbl = .ListObjects.Add(xlSrcRange, .Range("A1").Resize(lastRow, UBound(headers) + 1), , xlYes)
tbl.Name = "MyTable"
End With
' Apply formulas to columns
For i = 0 To UBound(formulas)
If formulas(i) <> "" Then
tbl.ListColumns(i + 1).DataBodyRange.Formula = formulas(i)
End If
Next i
End Sub
Adjust the headers and formulas arrays to match the column headers and formulas you want to use in your table. The formulas should be written as if you are working with structured references in Excel.
Close the VBA editor.
Press ALT + F8 to open the "Macro" dialog box, select CreateTable, and click "Run". This will create the table with the specified headers and formulas in the active worksheet.
You can run this VBA code whenever you want to create a new table on demand. Just make sure to set the active worksheet to the desired location before running the macro.
Customize the headerFormulaDict by adding your own header-formula pairs. The key of the dictionary represents the header name, and the value represents the formula for that column. If there is no formula for a column, use an empty string as the value.
Close the VBA editor.
Press ALT + F8 to open the "Macro" dialog box, select CreateTable, and click "Run". This will create the table with the specified headers and formulas in the active worksheet.
By using a Dictionary, you can easily manage a large number of columns, making your code more efficient and maintainable.
Follow the same steps to open the VBA editor and insert a new module.
Copy and paste the following code into the module:
Function CreateTableDictionary() As Object
Dim headerFormulaDict As Object
' Create a dictionary to store headers and formulas
Set headerFormulaDict = CreateObject("Scripting.Dictionary")
' Add headers and formulas to the dictionary
headerFormulaDict("Header1") = ""
headerFormulaDict("Header2") = ""
headerFormulaDict("Header3") = "=[@Header1] * [@Header2]"
' Add more headers and formulas as needed
Set CreateTableDictionary = headerFormulaDict
End Function
Sub CreateTable(headerFormulaDict As Object)
Dim ws As Worksheet
Dim tbl As ListObject
Dim key As Variant
Dim i As Long
' Set the active worksheet
Set ws = ActiveSheet
' Create a table with specified headers
With ws
.Cells(1, 1).Resize(1, headerFormulaDict.Count).Value = headerFormulaDict.Keys
Set tbl = .ListObjects.Add(xlSrcRange, .Range("A1").Resize(1, headerFormulaDict.Count), , xlYes)
tbl.Name = "MyTable"
End With
' Apply formulas to columns
i = 1
For Each key In headerFormulaDict.Keys
If headerFormulaDict(key) <> "" Then
tbl.ListColumns(i).DataBodyRange.Formula = headerFormulaDict(key)
End If
i = i + 1
Next key
End Sub
Customize the CreateTableDictionary function by adding your own header-formula pairs to the dictionary. The key of the dictionary represents the header name, and the value represents the formula for that column.
Close the VBA editor.
To create a table:
Press ALT + F11 to open the VBA editor.
Click Insert > Module to insert a new module.
Copy and paste the following code into the module:
Sub CreateNewTable()
Dim headerFormulaDict As Object
Set headerFormulaDict = CreateTableDictionary()
CreateTable headerFormulaDict
End Sub
Close the VBA editor.
Press ALT + F8 to open the "Macro" dialog box, select CreateNewTable, and click "Run". This will create the table with the specified headers and formulas in the active worksheet.
By separating the table creation and dictionary population, you can easily create different tables by modifying the CreateTableDictionary function or creating new functions with different header-formula pairs.
existingTableName
Here's the updated code:
Sub CreateTable(headerFormulaDict As Object, Optional existingTableName As String = "")
Dim ws As Worksheet
Dim tbl As ListObject
Dim key As Variant
Dim i As Long
Dim newTable As Boolean
' Set the active worksheet
Set ws = ActiveSheet
newTable = (existingTableName = "")
' Check if the existing table name is provided
If Not newTable Then
On Error Resume Next
Set tbl = ws.ListObjects(existingTableName)
On Error GoTo 0
If tbl Is Nothing Then
MsgBox "Table not found. Please check the table name."
Exit Sub
End If
End If
If newTable Then
' Create a table with specified headers
With ws
.Cells(1, 1).Resize(1, headerFormulaDict.Count).Value = headerFormulaDict.Keys
Set tbl = .ListObjects.Add(xlSrcRange, .Range("A1").Resize(1, headerFormulaDict.Count), , xlYes)
tbl.Name = "MyTable"
End With
Else
' Insert columns into the existing table
For i = 1 To headerFormulaDict.Count
tbl.ListColumns.Add Position:=tbl.ListColumns.Count + 1
tbl.HeaderRowRange.Cells(1, tbl.ListColumns.Count).Value = headerFormulaDict.Keys()(i - 1)
Next i
End If
' Apply formulas to columns
i = 1
For Each key In headerFormulaDict.Keys
If headerFormulaDict(key) <> "" Then
tbl.ListColumns(i).DataBodyRange.Formula = headerFormulaDict(key)
End If
i = i + 1
Next key
End Sub
To create a new table, you can use the same CreateNewTable subroutine from the previous answer. To insert columns into an existing table, create a new subroutine called InsertColumnsToExistingTable:
Sub InsertColumnsToExistingTable()
Dim headerFormulaDict As Object
Dim existingTableName As String
Set headerFormulaDict = CreateTableDictionary()
' Replace "ExistingTableName" with the name of the table you want to insert columns into
existingTableName = "ExistingTableName"
CreateTable headerFormulaDict, existingTableName
End Sub
Replace ExistingTableName with the name of the table you want to insert columns into. To run this subroutine, press ALT + F8, select InsertColumnsToExistingTable, and click "Run".
This updated code allows you to create a new table or insert columns into an existing table using the same CreateTable subroutine.
To achieve this, you can modify the CreateTableDictionary function to include a group and position information for each column. Then, update the CreateTable subroutine to insert columns based on the group and position information.
First, update the CreateTableDictionary function:
Function CreateTableDictionary() As Object
Dim headerFormulaDict As Object
' Create a dictionary to store headers, formulas, groups, and positions
Set headerFormulaDict = CreateObject("Scripting.Dictionary")
' Add headers, formulas, groups, and positions to the dictionary
' Use the format: Array(Formula, Group, Position_Column_Name)
headerFormulaDict("Header1") = Array("", "", "")
headerFormulaDict("Header2") = Array("", "Group1", "ExistingHeader1")
headerFormulaDict("Header3") = Array("=[@Header1] * [@Header2]", "Group1", "ExistingHeader2")
' Add more headers and formulas as needed
Set CreateTableDictionary = headerFormulaDict
End Function
Now, update the CreateTable subroutine to handle groups and positions:
Sub CreateTable(headerFormulaDict As Object, Optional existingTableName As String = "")
Dim ws As Worksheet
Dim tbl As ListObject
Dim key As Variant
Dim i As Long, position As Long
Dim newTable As Boolean
Dim formula As String, group As String, positionColumn As String
' Set the active worksheet
Set ws = ActiveSheet
newTable = (existingTableName = "")
' Check if the existing table name is provided
If Not newTable Then
On Error Resume Next
Set tbl = ws.ListObjects(existingTableName)
On Error GoTo 0
If tbl Is Nothing Then
MsgBox "Table not found. Please check the table name."
Exit Sub
End If
End If
If newTable Then
' Create a table with specified headers
With ws
.Cells(1, 1).Resize(1, headerFormulaDict.Count).Value = headerFormulaDict.Keys
Set tbl = .ListObjects.Add(xlSrcRange, .Range("A1").Resize(1, headerFormulaDict.Count), , xlYes)
tbl.Name = "MyTable"
End With
Else
' Insert columns into the existing table based on group and position information
For Each key In headerFormulaDict.Keys
formula = headerFormulaDict(key)(0)
group = headerFormulaDict(key)(1)
positionColumn = headerFormulaDict(key)(2)
If group <> "" And positionColumn <> "" Then
On Error Resume Next
position = tbl.ListColumns(positionColumn).Index + 1
On Error GoTo 0
If position = 0 Then
MsgBox "Position column not found. Please check the column name."
Exit Sub
End If
Else
position = tbl.ListColumns.Count + 1
End If
tbl.ListColumns.Add Position:=position
tbl.HeaderRowRange.Cells(1, position).Value = key
If formula <> "" Then
tbl.ListColumns(position).DataBodyRange.Formula = formula
End If
Next key
End If
End Sub
In the CreateTableDictionary function, you now define the formula, group, and position column name for each header. When you run the InsertColumnsToExistingTable subroutine, it will insert columns into the existing table based on the specified position column name.
Keep in mind that you need to replace the
To store the formatting information for each column group, you can create another dictionary for storing group formatting information. You can then use this dictionary to apply the formatting to the corresponding columns.
First, create a new function CreateGroupFormattingDictionary to store the formatting information for each group:
Function CreateGroupFormattingDictionary() As Object
Dim groupFormattingDict As Object
' Create a dictionary to store formatting information for each group
Set groupFormattingDict = CreateObject("Scripting.Dictionary")
' Add formatting information for each group
' Use the format: Array(HeadingCellColor)
groupFormattingDict("Group1") = Array(RGB(255, 0, 0))
groupFormattingDict("Group2") = Array(RGB(0, 255, 0))
' Add more group formatting information as needed
Set CreateGroupFormattingDictionary = groupFormattingDict
End Function
Next, update the CreateTable subroutine to apply the formatting information based on the group:
Sub CreateTable(headerFormulaDict As Object, Optional existingTableName As String = "")
Dim ws As Worksheet
Dim tbl As ListObject
Dim key As Variant
Dim i As Long, position As Long
Dim newTable As Boolean
Dim formula As String, group As String, positionColumn As String
Dim groupFormattingDict As Object
Dim headingCellColor As Long
' Set the active worksheet
Set ws = ActiveSheet
' Create the group formatting dictionary
Set groupFormattingDict = CreateGroupFormattingDictionary()
newTable = (existingTableName = "")
' Check if the existing table name is provided
If Not newTable Then
On Error Resume Next
Set tbl = ws.ListObjects(existingTableName)
On Error GoTo 0
If tbl Is Nothing Then
MsgBox "Table not found. Please check the table name."
Exit Sub
End If
End If
If newTable Then
' Create a table with specified headers
With ws
.Cells(1, 1).Resize(1, headerFormulaDict.Count).Value = headerFormulaDict.Keys
Set tbl = .ListObjects.Add(xlSrcRange, .Range("A1").Resize(1, headerFormulaDict.Count), , xlYes)
tbl.Name = "MyTable"
End With
Else
' Insert columns into the existing table based on group and position information
For Each key In headerFormulaDict.Keys
formula = headerFormulaDict(key)(0)
group = headerFormulaDict(key)(1)
positionColumn = headerFormulaDict(key)(2)
If group <> "" And positionColumn <> "" Then
On Error Resume Next
position = tbl.ListColumns(positionColumn).Index + 1
On Error GoTo 0
If position = 0 Then
MsgBox "Position column not found. Please check the column name."
Exit Sub
End If
Else
position = tbl.ListColumns.Count + 1
End If
tbl.ListColumns.Add Position:=position
tbl.HeaderRowRange.Cells(1, position).Value = key
If formula <> "" Then
tbl.ListColumns(position).DataBodyRange.Formula = formula
End If
' Apply formatting based on the group
If groupFormattingDict.Exists(group) Then
headingCellColor = groupFormattingDict(group)(0)
tbl.HeaderRowRange.Cells(1, position).Interior.Color = headingCellColor
End If
Next key
End If
End Sub
``
You can modify the CreateGroupFormattingDictionary function to include all the formatting properties you wish to set for the column headers. Then, update the CreateTable subroutine to apply these formatting properties to the corresponding columns.
First, update the CreateGroupFormattingDictionary function:
Function CreateGroupFormattingDictionary() As Object
Dim groupFormattingDict As Object
' Create a dictionary to store formatting information for each group
Set groupFormattingDict = CreateObject("Scripting.Dictionary")
' Add formatting information for each group
' Use the format: Array(Pattern, PatternColorIndex, Color, TintAndShade, PatternTintAndShade)
groupFormattingDict("Group1") = Array(xlSolid, xlAutomatic, RGB(255, 0, 0), 0, 0)
groupFormattingDict("Group2") = Array(xlSolid, xlAutomatic, RGB(0, 255, 0), 0, 0)
' Add more group formatting information as needed
Set CreateGroupFormattingDictionary = groupFormattingDict
End Function
Now, update the CreateTable subroutine to apply the formatting properties based on the group:
Sub CreateTable(headerFormulaDict As Object, Optional existingTableName As String = "")
Dim ws As Worksheet
Dim tbl As ListObject
Dim key As Variant
Dim i As Long, position As Long
Dim newTable As Boolean
Dim formula As String, group As String, positionColumn As String
Dim groupFormattingDict As Object
Dim headerCell As Range
' Set the active worksheet
Set ws = ActiveSheet
' Create the group formatting dictionary
Set groupFormattingDict = CreateGroupFormattingDictionary()
newTable = (existingTableName = "")
' Check if the existing table name is provided
If Not newTable Then
On Error Resume Next
Set tbl = ws.ListObjects(existingTableName)
On Error GoTo 0
If tbl Is Nothing Then
MsgBox "Table not found. Please check the table name."
Exit Sub
End If
End If
If newTable Then
' Create a table with specified headers
With ws
.Cells(1, 1).Resize(1, headerFormulaDict.Count).Value = headerFormulaDict.Keys
Set tbl = .ListObjects.Add(xlSrcRange, .Range("A1").Resize(1, headerFormulaDict.Count), , xlYes)
tbl.Name = "MyTable"
End With
Else
' Insert columns into the existing table based on group and position information
For Each key In headerFormulaDict.Keys
formula = headerFormulaDict(key)(0)
group = headerFormulaDict(key)(1)
positionColumn = headerFormulaDict(key)(2)
If group <> "" And positionColumn <> "" Then
On Error Resume Next
position = tbl.ListColumns(positionColumn).Index + 1
On Error GoTo 0
If position = 0 Then
MsgBox "Position column not found. Please check the column name."
Exit Sub
End If
Else
position = tbl.ListColumns.Count + 1
End If
tbl.ListColumns.Add Position:=position
tbl.HeaderRowRange.Cells(1, position).Value = key
If formula <> "" Then
tbl.ListColumns(position).DataBodyRange.Formula = formula
End If
' Apply formatting based on the group
If groupFormattingDict.Exists(group) Then
Set headerCell = tbl
In Module2, add the following code:
' Module2
Function CreateTableDictionary() As Object
Dim headerFormulaDict As Object
' Create a dictionary to store headers, formulas, groups, and positions
Set headerFormulaDict = CreateObject("Scripting.Dictionary")
' Add headers, formulas, groups, and positions to the dictionary
' Use the format: Array(Formula, Group, Position_Column_Name)
headerFormulaDict("Header1") = Array("", "", "")
headerFormulaDict("Header2") = Array("", "Group1", "ExistingHeader1")
headerFormulaDict("Header3") = Array("=[@Header1] * [@Header2]", "Group1", "ExistingHeader2")
' Add more headers and formulas as needed
Set CreateTableDictionary = headerFormulaDict
End Function
Function CreateGroupFormattingDictionary() As Object
Dim groupFormattingDict As Object
' Create a dictionary to store formatting information for each group
Set groupFormattingDict = CreateObject("Scripting.Dictionary")
' Add formatting information for each group
' Use the format: Array(Pattern, PatternColorIndex, Color, TintAndShade, PatternTintAndShade)
groupFormattingDict("Group1") = Array(xlSolid, xlAutomatic, RGB(255, 0, 0), 0, 0)
groupFormattingDict("Group2") = Array(xlSolid, xlAutomatic, RGB(0, 255, 0), 0, 0)
' Add more group formatting information as needed
Set CreateGroupFormattingDictionary = groupFormattingDict
End Function
In Module1, add the following code:
' Module1
Sub CreateTable(headerFormulaDict As Object, Optional existingTableName As String = "")
' ... (the CreateTable subroutine code remains unchanged)
End Sub
Sub RunCreateTable()
Dim headerFormulaDict As Object
Dim groupFormattingDict As Object
Set headerFormulaDict = CreateTableDictionary()
CreateTable headerFormulaDict, "ExistingTableName" ' Replace "ExistingTableName" with your table's name
End Sub
Now, non-technical users can easily update the formulas and formatting by modifying the code in Module2 without worrying about the rest of the VBA code in Module1. To create or update the table, they can run the RunCreateTable subroutine from Module1.
In Module2, update the code to create an instance of the TableBuilder class and set the dictionaries:
' Module2
Function CreateTableBuilder() As TableBuilder
Dim builder As New TableBuilder
Set builder.HeaderFormulaDict = CreateTableDictionary()
Set builder.GroupFormattingDict = CreateGroupFormattingDictionary()
Set CreateTableBuilder = builder
End Function
In Module1, update the RunCreateTable subroutine to use the TableBuilder class:
' Module1
Sub RunCreateTable()
Dim builder As TableBuilder
Set builder = CreateTableBuilder()
builder.CreateTable "ExistingTableName" ' Replace "ExistingTableName" with your table's name
End Sub
Now, you can create multiple instances of the TableBuilder class with different configurations, making it easier to manage and reuse. Non-technical users can still update the formulas and formatting in Module2 without worrying about the rest of the VBA code.
Here is the complete code organized into the different modules and class:
In Module1, add the following code:
' Module1
Sub RunCreateTable()
Dim builder As TableBuilder
Set builder = CreateTableBuilder()
builder.CreateTable "ExistingTableName" ' Replace "ExistingTableName" with your table's name
End Sub
In Module2, add the following code:
' Module2
Function CreateTableDictionary() As Object
Dim headerFormulaDict As Object
' Create a dictionary to store headers, formulas, groups, and positions
Set headerFormulaDict = CreateObject("Scripting.Dictionary")
' Add headers, formulas, groups, and positions to the dictionary
' Use the format: Array(Formula, Group, Position_Column_Name)
headerFormulaDict("Header1") = Array("", "", "")
headerFormulaDict("Header2") = Array("", "Group1", "ExistingHeader1")
headerFormulaDict("Header3") = Array("=[@Header1] * [@Header2]", "Group1", "ExistingHeader2")
' Add more headers and formulas as needed
Set CreateTableDictionary = headerFormulaDict
End Function
Function CreateGroupFormattingDictionary() As Object
Dim groupFormattingDict As Object
' Create a dictionary to store formatting information for each group
Set groupFormattingDict = CreateObject("Scripting.Dictionary")
' Add formatting information for each group
' Use the format: Array(Pattern, PatternColorIndex, Color, TintAndShade, PatternTintAndShade)
groupFormattingDict("Group1") = Array(xlSolid, xlAutomatic, RGB(255, 0, 0), 0, 0)
groupFormattingDict("Group2") = Array(xlSolid, xlAutomatic, RGB(0, 255, 0), 0, 0)
' Add more group formatting information as needed
Set CreateGroupFormattingDictionary = groupFormattingDict
End Function
Function CreateTableBuilder() As TableBuilder
Dim builder As New TableBuilder
Set builder.HeaderFormulaDict = CreateTableDictionary()
Set builder.GroupFormattingDict = CreateGroupFormattingDictionary()
Set CreateTableBuilder = builder
End Function
Create a new class module, name it TableBuilder, and add the following code:
' TableBuilder Class
Private pHeaderFormulaDict As Object
Private pGroupFormattingDict As Object
Property Get HeaderFormulaDict() As Object
Set HeaderFormulaDict = pHeaderFormulaDict
End Property
Property Let HeaderFormulaDict(headerFormulaDict As Object)
Set pHeaderFormulaDict = headerFormulaDict
End Property
Property Get GroupFormattingDict() As Object
Set GroupFormattingDict = pGroupFormattingDict
End Property
Property Let GroupFormattingDict(groupFormattingDict As Object)
Set pGroupFormattingDict = groupFormattingDict
End Property
Sub CreateTable(Optional existingTableName As String = "")
Dim ws As Worksheet
Dim tbl As ListObject
Dim key As Variant
Dim i As Long, position As Long
Dim newTable As Boolean
Dim formula As String, group As String, positionColumn As String
Dim headerCell As Range
' Set the active worksheet
Set ws = ActiveSheet
newTable = (existingTableName = "")
' Check if the existing table name is provided
If Not newTable Then
On Error Resume Next
Set tbl = ws.ListObjects(existingTableName)
On Error GoTo 0
If tbl Is Nothing Then
MsgBox "Table not found. Please