What I want , If I click on the toggle button must change color to vbgreen and if click it again change color to vbred for the code below, where and f

Carlosjunior

New Member
Joined
Jun 7, 2023
Messages
7
Office Version
  1. 2021
Platform
  1. Windows
Private Sub ToggleButton1_Click()
' hide or show rows 2 to 4 if button is clicked
Sheets("Sheet1").Range("2:4").EntireRow.Hidden = ToggleButton1.Value
End Sub


Private Sub UserForm_Activate()
' toggle button state based on hidden state of rows
ToggleButton1.Value = Sheets("Sheet1").Range("2:4").EntireRow.Hidden
End Sub


------------------------------------------------------------------------------------------------------------------


For the code above for me its working. What I want now is, if i click on the toggle button must change color to vbgreen and if click it again change color to vbred
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Long time, possibly something like this you can work with.

VBA Code:
Private Sub ToggleButton1_Click()
With Me.ToggleButton1
If .Value = True Then
.BackColor = vbGreen
.Caption = "On"
MsgBox "code for On"
ElseIf .Value = False Then
.BackColor = vbRed
.Caption = "Off"
MsgBox "code for Off"
End If
End With
End Sub

Private Sub UserForm_Activate()
Me.ToggleButton1 = True
End Sub
 
Upvote 0
Long time, possibly something like this you can work with.

VBA Code:
Private Sub ToggleButton1_Click()
With Me.ToggleButton1
If .Value = True Then
.BackColor = vbGreen
.Caption = "On"
MsgBox "code for On"
ElseIf .Value = False Then
.BackColor = vbRed
.Caption = "Off"
MsgBox "code for Off"
End If
End With
End Sub

Private Sub UserForm_Activate()
Me.ToggleButton1 = True
End Sub
Thank u very kindly @davesexcel . Done & Dusted.
 
Upvote 0

Forum statistics

Threads
1,214,625
Messages
6,120,598
Members
448,973
Latest member
ksonnia

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