Add buttons automatically based on table values. Each button press adds button label to cell L2 on dashboard

allen675

New Member
Joined
Oct 7, 2023
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Good day to you all,

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"
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
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
ActiveX controls, such as your CommandButton, do not contain the OnAction property. Instead, they use sheet driven event handlers. So this means that your code would have to insert the appropriate event handler for each new CommandButton. And it would need to delete the event handlers when it deletes the buttons. All of this would require that you allow access to the VBA project object model. So while your code can be amended accordingly, it would make things much more complex.

As an alternative, though, I would suggest that you use Buttons from Form controls instead. As such, your code could be easily amended as follows...

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.Buttons.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.Buttons.Add(Left:=ButtonLeft, Top:=ButtonTop, Width:=100, Height:=20)
         
            ' Set the button's label to the cell's value
            NewButton.Caption = Cell.Value
         
            ' Assign a macro to the button
            NewButton.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

Does this help?
 
Last edited:
Upvote 0
Solution
Just out of curiosity rather than the values going into a cell would it be possible to put them into a listbox on the worksheet?
 
Upvote 0
Sure, first add a ListBox from the Form controls group to your worksheet called "Dashboard". Then you might want to link your ListBox to a worksheet cell (right-click the ListBox >> Format Control >> Control >> Cell link) so that you can determine which item is selected, if any. Then amend your code as follows...

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.Buttons.Delete
    
    ' Clear the items from the ListBox
    DashboardSheet.ListBoxes("List Box 1").RemoveAllItems
    
    ' 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.Buttons.Add(Left:=ButtonLeft, Top:=ButtonTop, Width:=100, Height:=20)
            
            ' Set the button's label to the cell's value
            NewButton.Caption = Cell.Value
            
            ' Assign a macro to the button
            NewButton.OnAction = "AddValueToListBox"
            
            ' 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 AddValueToListBox()
    Dim ButtonLabel As String
    
    ' Get the label of the clicked button
    ButtonLabel = ActiveSheet.Shapes(Application.Caller).TextFrame.Characters.Caption
    
    ' Add the label to the listbox
    ThisWorkbook.Sheets("Dashboard").ListBoxes("List Box 1").AddItem ButtonLabel
    
End Sub

Hope this helps!
 
Upvote 0
@Domenic Hi sorry for the delay! I have followed your guide above and I am hitting an error as follows:

Run-time error '1004': The item wasnt found. This happens on the his line:

VBA Code:
DashboardSheet.ListBoxes("ListBox777").RemoveAllItems

I'm wondering if it is the listbox name?
 
Upvote 0
Yes, it's likely the listbox name. ctrl-click the listbox, and check the Name Box to the left of the formula bar. Is the name correct?
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,241
Members
452,622
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top