Example 1 – Changing the Toggle Button Color When Pressed
Steps
- Go to the Developer tab and click Insert. (Click the link to enable the Developer tab.)
- In Form Controls List, click Toggle.
- Draw the toggle button in the worksheet. The drawing area will determine the dimension of the toggle button.
- Right-click the Toggle Button and click View Code.
- Enter the following code:
Private Sub ToggleButton1_Click()
ToggleButton1.Caption = "Toggle Button"
ToggleButton1.BackColor = RGB(146, 208, 80)
ToggleButton1.FontSize = 16
ToggleButton1.ForeColor = RGB(0, 0, 0)
End Sub
- Click Save.
- Click Run.
The text is visible.
- Close the Code Editor.
If you click the toggle button, the color changes.
The toggle button is working.
Read More: How to Change Cell Value Using Toggle Button in Excel
Example 2 – Changing the Toggle Button Color When it is released
Steps
- Go to the Developer tab and click Insert. (Click the link to enable the Developer tab.)
- In Form Controls List, click Toggle.
- Draw the toggle button in the worksheet.
- Click the toggle button and go to the Developer tab again.
- In Developer, click View Code.
- Enter the following code:
Private Sub ToggleButton1_Click()
If ToggleButton1.Value = True Then
ToggleButton1.Caption = "Toggle Button"
ToggleButton1.BackColor = RGB(146, 208, 80)
ToggleButton1.FontSize = 16
ToggleButton1.ForeColor = RGB(0, 0, 0)
Else
ToggleButton1.Caption = "Toggle Button"
ToggleButton1.BackColor = RGB(238, 236, 225)
ToggleButton1.FontSize = 16
ToggleButton1.ForeColor = RGB(0, 0, 0)
End If
End Sub
- Click Save.
- Click Run.
The toggle button is in the worksheet.
- If you click and hold the toggle button, the color changes.
- If you release the button, the color changes as well.
Example 3 – Changing the Toggle Button Color Based on a Condition
Steps
- Go to the Developer tab and click Insert.
- In Activex Controls List, click Toggle.
- Draw the toggle button in the worksheet.
- Right-click the Toggle Button and click View Code.
- Enter the following code:
Private Sub ToggleButton1_Click()
If ToggleButton1.Value = True Then
If Cells(5, 2).Value > Cells(5, 3).Value Then
With Me.ToggleButton1
Me.ToggleButton1.Caption = "Toggle Button"
.BackColor = IIf(.Value, vbRed, vbButtonFace)
End With
ElseIf Cells(5, 2).Value = Cells(5, 3).Value Then
With Me.ToggleButton1
Me.ToggleButton1.Caption = "Toggle Button"
.BackColor = IIf(.Value, vbGreen, vbButtonFace)
End With
ElseIf Cells(5, 2).Value < Cells(5, 3).Value Then
With Me.ToggleButton1
Me.ToggleButton1.Caption = "Toggle Button"
.BackColor = IIf(.Value, vbYellow, vbButtonFace)
End With
End If
End If
End Sub
The toggle button is formatted.
- In Number 1 value, 10 was entered, and 16 in Number 2.
- Click the ToggleButton.
- Color changes to yellow.
- Both in Number 1 and Number 2, 16 was entered.
- Click the ToggleButton.
- The color changes to Green.
- In Number 1 value, 20 was entered, and 16 in Number 2.
- Click the ToggleButton.
- The color changes to Red.
Example 4 – Changing the Toggle Button Color Based on the Cell Value Type
Steps
- Go to the Developer tab and click Insert.
- In Activex Controls List, click Toggle.
- Draw the toggle button in the worksheet.
- Right-click the Toggle Button and click View Code.
- Enter the following code:
Private Sub ToggleButton1_Click()
If WorksheetFunction.IsText(Sheets("Changing Color on Cell Value").Cells(5, 2)) = True Then
ToggleButton1.Caption = "Toggle Button"
ToggleButton1.BackColor = RGB(146, 208, 80)
ToggleButton1.FontSize = 16
ToggleButton1.ForeColor = RGB(0, 0, 0)
ElseIf WorksheetFunction.IsNumber(Sheets("Changing Color on Cell Value").Cells(5, 2).Value) = True Then
ToggleButton1.Caption = "Toggle Button"
ToggleButton1.BackColor = RGB(165, 42, 42)
ToggleButton1.FontSize = 16
ToggleButton1.ForeColor = RGB(0, 0, 0)
ElseIf IsDate(Sheets("Changing Color on Cell Value").Cells(5, 2).Value) = True Then
ToggleButton1.Caption = "Toggle Button"
ToggleButton1.BackColor = RGB(255, 102, 255)
ToggleButton1.FontSize = 16
ToggleButton1.ForeColor = RGB(0, 0, 0)
ElseIf IsEmpty(Sheets("Changing Color on Cell Value").Cells(5, 2).Value) = True Then
ToggleButton1.Caption = "Toggle Button"
ToggleButton1.BackColor = RGB(242, 227, 227)
ToggleButton1.FontSize = 16
ToggleButton1.ForeColor = RGB(0, 0, 0)
End If
End Sub
The toggle button is displayed in the worksheet.
- Enter Random Text in B5.
- Click the toggle button.
- It will turn green.
- Enter 15 in B5.
- Click the toggle button.
- It will turn Red.
- Enter the date 4/12/22 in B5.
- Click the toggle button.
- It will turn Purple.
- Delete B5.
- Click the toggle button.
- It will turn White.
Download Practice Workbook
Download the practice workbook.
Get FREE Advanced Excel Exercises with Solutions!