Link to home
Start Free TrialLog in
Avatar of Mike Rudolph
Mike RudolphFlag for United States of America

asked on

Set ALL ToggleButton state using VBA code?

Can't seem to find code to set the value of all toggle buttons on a form to true or false.

I have over 50 toggle buttons on the form so need a way to do this in a For Loop.
ASKER CERTIFIED SOLUTION
Avatar of Mike Rudolph
Mike Rudolph
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Roy Cox
I would suggest a slight change

Dim ctl As MSForms.Control
For Each ctl In Me.Controls
If TypeName(ctl) = "ToggleButton" Then
ctl.Value = True
End If
Next ctl

Open in new window


A long time ago I used similar code but had issues the addition of MsForms.Controls cured the issues and I've used it ever since. In fact I've just found some code that I posted previously that changes the colour of the buttons as well

Option Explicit
Sub Tog()
' declare variable for the Control
    Dim oCtl As MSForms.Control
    'loop through controls in UserForm
    For Each oCtl In Me.Controls
        'if it's a Togglebutton and not the one that called the code make the changes
        If TypeName(oCtl) = "ToggleButton" And oCtl.Name <> ActiveControl.Name Then
            'make sure it's value is false
            oCtl.Value = False
            'change colour back to standard button colour
            oCtl.BackColor = &H8000000F
        End If
    Next oCtl
    'change the back colour of the button that called the code to red(ish)
    ActiveControl.BackColor = vbRed
End Sub

Open in new window

Avatar of Mike Rudolph

ASKER

Roy, the change color options was helpful! Thank you!
Pleased to help, do use MSForms.Control instead of just Control though.