Vba Userform control, common code for two or more togglebuttons

Udaya Raj

New Member
Joined
Mar 30, 2014
Messages
8
Hi I used to only surf over this site but now I am here with my problem in user form control
I have nearly 200 toggle buttons in a user form and I want half of them "Togglebutton1" to "Togglebutton100" will toggle between "Wall" and "Slab" and half of them "Togglebutton101" to "Togglebutton200" will toggle between "Unit-1" and "Unit-2". so for each buttons click from 1 to 100 will have a same code and 101 to 200 will have a same click action. I was just wondering if i have to assign individual click action for each togglebutton or is there any way to loop between them.

code is like this

HTML:
'for togglebuttons 1 to 100

Private Sub ToggleButton1_Click()
    If ToggleButton1.Value = False Then
    ToggleButton1.Caption = "Wall"
    Else
    ToggleButton1.Caption = "Slab"
    End If
End Sub

'for togglebuttons 101 to 200

Private Sub ToggleButton101_Click()
    If ToggleButton101.Value = False Then
    ToggleButton101.Caption = "Unit-1"
    Else
    ToggleButton101.Caption = "Unit-2"
    End If
End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

Kyle123

Well-known Member
Joined
Jan 24, 2012
Messages
2,752
You need a class to respond to the events raised. This should cover it:

Class1
Code:
Public WithEvents toggle As MSForms.ToggleButton
Public slab As Boolean


Private Sub toggle_Click()
    With Me.toggle
        If slab Then
            If .Value Then .Caption = "Slab" Else .Caption = "Wall"
        Else
            If .Value Then .Caption = "Unit-1" Else .Caption = "Unit-2"
        End If
    End With
End Sub


UserForm
Code:
Dim coll As Collection


Private Sub UserForm_Initialize()
    Dim toggle As Class1
    Dim ctl As Control
    Dim x As Long
    
    Set coll = New Collection
    
    For x = 1 To 200
        Set toggle = New Class1
        Set toggle.toggle = Me.Controls("ToggleButton" & x)
        If x > 100 Then toggle.slab = True
        coll.Add toggle
    Next x
    
End Sub
 

Udaya Raj

New Member
Joined
Mar 30, 2014
Messages
8
Thanks Kyle for your help. I tried above code but it seems it is doing nothing.. as I am first time using class module can you tell me how this code works??
 

Kyle123

Well-known Member
Joined
Jan 24, 2012
Messages
2,752
Can you please post your code and where you've put it all?
 

Udaya Raj

New Member
Joined
Mar 30, 2014
Messages
8
From the Menu Bar_Insert_Class Module, I put class module there and in the userform I put the userform initialize code. when I click "wall" button or "#1" button it does not toggles. Again I want if I click "wall" button it should toggle to "slab" and when I click "#1" button it should toggle to "#2". sorry for disappointing you
 
Last edited:

Udaya Raj

New Member
Joined
Mar 30, 2014
Messages
8
Private Sub UserForm_Initialize()
On Error Resume Next
Dim i, j As Integer
Dim coll As Collection

Dim toggle As Class1
Dim ctl As Control
Dim x As Long

Set coll = New Collection

For x = 1 To 200
Set toggle = New Class1
Set toggle.toggle = Me.Controls("ToggleButton" & x)
If x > 100 Then toggle.slab = True
coll.Add toggle
Next x

' Level Selection combo button
For i = 1 To 74
With Me.Controls("ComboBox" & i)
For j = 1 To 8
.AddItem j
Next j
End With
Next i

'Toggle Button (wall/Slab) and (unit-1/unit-2)
For i = 1 To 74
Me.Controls("ToggleButton" & i).Value = False
Me.Controls("ToggleButton" & i).Caption = "Wall"
Next i

For i = 101 To 174
Me.Controls("ToggleButton" & i).Value = False
Me.Controls("ToggleButton" & i).Caption = "#1"
Next i

' Text box clear

For i = 1 To 74
With Me.Controls("TextBox" & i)
.Value = "10"
End With
Next i

For i = 101 To 174
With Me.Controls("TextBox" & i)
.Value = "BLOCK"
End With
Next i

For i = 201 To 274
With Me.Controls("TextBox" & i)
.Value = "WORK DESCRIPTION HERE"
End With
Next i

End Sub

'In class module
Public WithEvents toggle As MSForms.ToggleButton
Public slab As Boolean


Private Sub toggle_Click()
With Me.toggle
If slab Then
If .Value Then .Caption = "Slab" Else .Caption = "Wall"
Else
If .Value Then .Caption = "Unit-1" Else .Caption = "Unit-2"
End If
End With
End Sub
HTML:
 

Forum statistics

Threads
1,171,047
Messages
5,873,466
Members
432,981
Latest member
DMcDaniel

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
Top