Good day to you all,
The tile states it all really. This is my code so far:
I have this code in a module. I am getting stuck at
and get a run-time error 438 Object doesnt support this property or method. Think its something really simple that I am missing but cant work it out
Your help would be greatly appreciated. Thanking you in advance
The tile states it all really. This is my code so far:
VBA Code:
Sub AddButtonsInGrid()
Dim SourceSheet As Worksheet
Dim DashboardSheet As Worksheet
Dim Table As ListObject
Dim ButtonLeft As Double
Dim ButtonTop As Double
Dim Cell As Range
Dim NewButton As Object
Dim ButtonSpacing As Double
' Set the source and dashboard sheets
Set SourceSheet = ThisWorkbook.Sheets("LookupLists")
Set DashboardSheet = ThisWorkbook.Sheets("Dashboard")
' Clear existing buttons on the dashboard (if any)
DashboardSheet.OLEObjects.Delete
' Initialize button positioning
ButtonLeft = 10
ButtonTop = 10
ButtonSpacing = 25
' Initialize the concatenated value
Dim ConcatenatedValue As String
ConcatenatedValue = ""
' Loop through the tables in the source sheet
For Each Table In SourceSheet.ListObjects
' Loop through the cells in the first column of the table's data body range (excluding headers)
For Each Cell In Table.ListColumns(1).DataBodyRange
' Create a button for each cell's value
Set NewButton = DashboardSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
Link:=False, DisplayAsIcon:=False, Left:=ButtonLeft, Top:=ButtonTop, Width:=100, Height:=20)
' Set the button's label to the cell's value
NewButton.Object.Caption = Cell.Value
' Assign a macro to the button
NewButton.Object.OnAction = "AddValueToConcatenatedValue"
' Adjust the left position for the next button in the same column
ButtonTop = ButtonTop + ButtonSpacing
Next Cell
' Move to the next column
ButtonTop = 10
ButtonLeft = ButtonLeft + 120
Next Table
' Add a cell to display the concatenated value (cell L2)
DashboardSheet.Range("L2").Value = ConcatenatedValue
End Sub
Sub AddValueToConcatenatedValue()
Dim ButtonLabel As String
' Get the label of the clicked button
ButtonLabel = ActiveSheet.Shapes(Application.Caller).TextFrame.Characters.Caption
' Update the concatenated value in cell L2
ThisWorkbook.Sheets("Dashboard").Range("L2").Value = ThisWorkbook.Sheets("Dashboard").Range("L2").Value & ", " & ButtonLabel
End Sub
I have this code in a module. I am getting stuck at
Code:
NewButton.Object.OnAction = "AddValueToConcatenatedValue"
Your help would be greatly appreciated. Thanking you in advance