How to Change the Color of the Toggle Button When Pressed in Excel – Examples

 

 

Example 1 – Changing Toggle Button Color When Pressed

Use the togglebutton.Click property.

Steps

  • Go to the Developer tab and click Insert. (Click the link to enable the Developer tab.)
  • In Form Controls List, click Toggle.

Changing Color of toggle button When pressed in Excel

  • Draw the toggle button in the worksheet. The drawing area will determine the dimension of the toggle button.

draw togglebutton shape to cheange toggle button color in Excel when pressed

  • 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.

toggle button color changed

Read More: How to Change Cell Value Using Toggle Button in Excel


Example 2 – Changing the Toggle Button Color When it is released

 

Color will change when you click the toggle button, and when you release it.

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.

Change togglebutton Color When Depressed in Excel

  • 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.

Change togglebutton Color Based on logical Condition in Excel

  • Draw the toggle button in the worksheet.
  • Right-click the Toggle Button and click View Code.

 

Click View code to open vba editor

  • 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.

toggle button color changed according to the logical caondition.


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.

 

Changing toggol button Color Based on Cell Value Type in Excel

  • 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 in the worksheet.

  • Enter Random Text in B5.
  • Click the toggle button.
  • It will turn green.

change color due to random text

  • 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.

change color for date input

  • Delete  B5.
  • Click the toggle button.
  • It will turn White.

toggle button color changed based on cell value type


Download Practice Workbook

Download the practice workbook.

Get FREE Advanced Excel Exercises with Solutions!
Rubayed Razib Suprov
Rubayed Razib Suprov

Rubayed Razib, holding a BSC degree in Naval Architecture & Engineering from Bangladesh University of Engineering and Technology, serves as a devoted member of the ExcelDemy project. He has contributed significantly by authoring numerous articles and showcasing proficiency in VBA. Razib efficiently automates Excel challenges using VBA macros and actively participates in the ExcelDemy forum, providing valuable solutions for user interface challenges. Apart from creating Excel tutorials, he is interested in Data Analysis with MS Excel,... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo