VBA easy way to code all toggle buttons

Jason Sawyer

New Member
Joined
May 20, 2009
Messages
3
Learning on my own, forgive stupidity please.

I have a spreadsheet with 100 toggle buttons that when true/false will show/hide the comments of a cell.

Private Sub ToggleButton1_Click()
Range(ToggleButton1.LinkedCell).Select
If ToggleButton1.Value = True Then
ActiveCell.Offset(0, 2).Range("A1").Select
ActiveCell.Comment.Visible = True
ElseIf ToggleButton1.Value = False Then
ActiveCell.Offset(0, 2).Range("A1").Select
ActiveCell.Comment.Visible = False
End If
End Sub

This works great. I would like to have the commands generic so that I can point all of the buttons to point to a ?macro? so I do not have to rename each ToggleButton#.

Private Sub ToggleButton1_Click()
Module1.ToggleCommand
End Sub
Private Sub ToggleButton2_Click()
Module1.ToggleCommand
End Sub

Sub ToggleCommand()
Range(ToggleButton.LinkedCell).Select
If ToggleButton.Value = True Then
ActiveCell.Offset(0, 2).Range("A1").Select
ActiveCell.Comment.Visible = True
ElseIf ToggleButton.Value = False Then
ActiveCell.Offset(0, 2).Range("A1").Select
ActiveCell.Comment.Visible = False
End If
End Sub

But this does not work. Am I just an idiot or can something like this be accomplished, or both? What I have so far, is pieced together from what I figured out playing with VBA and searching the web.

Thanks,
Jason
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Your code could be shortened to:

Code:
Private Sub ToggleButton1_Click()
    With ToggleButton1
        Range(.LinkedCell).Offset(0, 2).Comment.Visible = .Value
    End With
End Sub

That way the name of the ToggleButton appears only once. You could pass the ToggleButton as an object to another procedure, but you would still have to change the name for each button.
 
Upvote 0
Try:
Code:
Private Sub ToggleButton1_Click()
   ToggleCommand ToggleButton1
End Sub
Private Sub ToggleButton2_Click()
   ToggleCommand ToggleButton2
End Sub
Private Sub ToggleButton3_Click()
   ToggleCommand ToggleButton3
End Sub
Sub ToggleCommand(btn As MSForms.ToggleButton)
   With ActiveSheet
      .Range(btn.LinkedCell).Offset(0, 2).Comment.Visible = btn.Value
   End With
End Sub

You could also use a class module, if that fits your definition of easy!
 
Upvote 0
Example of a class module:

Code:
'Class module named ToggleClass
 
Public WithEvents ToggleGroup As MSForms.ToggleButton
 
Private Sub ToggleGroup_Click()
    With ToggleGroup
        Range(.LinkedCell).Offset(0, 2).Comment.Visible = .Value
    End With
End Sub
 
'ThisWorkbook module
 
Dim TBs() As New ToggleClass
 
Private Sub Workbook_Open()
    Dim TBCount As Integer
    Dim Ctrl As OLEObject
    TBCount = 0
    For Each Ctrl In Worksheets("Sheet1").OLEObjects
        If TypeName(Ctrl.Object) = "ToggleButton" Then
                TBCount = TBCount + 1
                ReDim Preserve TBs(1 To TBCount)
                Set TBs(TBCount).ToggleGroup = Ctrl.Object
        End If
    Next Ctrl
End Sub

After entering the code save, close and reopen the workbook and try it out by clicking a ToggleButton.
 
Upvote 0
Hi,

only passing by quicky so someone will need to finish this
I'm stuck on the classmodule code: see comment :confused:

advantages:
you need to code only once, even if the number of buttons changes
no code per button

create classmodule and accept defualt name "Class1"
Code:
Option Explicit
 
Public WithEvents GroupToggleButton As MSForms.ToggleButton
Private Sub GroupToggleButton_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    With GroupToggleButton
        'next line needs to be corrected
        'Range(.LinkedCell).Offset(0, 2).Comment.Visible = .Value
    End With
End Sub
in a normal module
Code:
Option Explicit
Dim ToggleButtonHandler() As New Class1
Private Sub Auto_Open()
Dim I
Dim SH As Worksheet
Dim CB As Object
I = 0
Set SH = Sheets("sheet1")
    For Each CB In SH.OLEObjects
        If TypeName(CB.Object) = "ToggleButton" Then
        I = I + 1
        ReDim Preserve ToggleButtonHandler(1 To I)
        Set ToggleButtonHandler(I).GroupToggleButton = CB.Object
        End If
    Next CB
End Sub

kind regards,
Erik
 
Upvote 0
I know this is a old post, but I had a similar situation and I was able to adapt this. Thank you.
 
Upvote 0

Forum statistics

Threads
1,215,012
Messages
6,122,682
Members
449,091
Latest member
peppernaut

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