How to Change Color of Toggle Button When Pressed in Excel

When you have a toggle button in your worksheet, you can use it to program certain things in your workbook. But at the same time, if the color of the toggle button also can be changed, it will enhance the functionality manifold. If you are curious to know how you can change the color of the toggle button when pressed, then this article may come in handy for you. In this article, we discuss how you can change the color of the toggle button when pressed in Excel with elaborate explanations.


Change Toggle Button Color When Pressed in Excel: 4 Suitable Examples

In this article, we are going to demonstrate how you can change the color of the toggle button in Excel. We also incorporated different kinds of criteria to make the color-changing process much more useful. For the sake of avoiding all sorts of misshapen, users are advised to use the Excel 365 version.


Example 1: Changing Toggle Button Color When Pressed

This example will show you how you can change the color of the toggle button with just a simple click. Here togglebutton.Click property will be used.

Steps

  • First, go to the Developer tab and click on Insert If you don’t have that, you have to enable the Developer tab.
  • From the Form Controls List, click on the Toggle Button.

Changing Color of toggle button When pressed in Excel

  • Then place the toggle button by drawing them on 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 on the Toggle Button and click on the View Code from the context menu.

  • A code editor will open, in that code editor, 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
  • After entering the code, Click on Save.
  • Then press the code run command.
  • Doing this will run the code.
  • You will immediately see that the code is run, and the text is visible.
  • Then close the Code Editor.
  • Now If you click on the toggle button, you will notice that the color is now changing as we click the toggle button.

  • This means our toggle button color change is working perfectly.

toggle button color changed

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


Example 2: Changing Toggle Button Color When Depressed

Compared to the previous example, this example will let you change color on two occasions.

First when you click the toggle button, and when you release the button.

Steps

  • First, go to the Developer tab and click on Insert. If you don’t have that, you have to enable the Developer tab.
  • From the Form Controls List, click on the Toggle Button.
  • Then place the toggle button by drawing them on the worksheet. The drawing area will determine the dimension of the ToggleButton.
  • Click on the newly created toggle button and then go to the Developer tab again.
  • From the Developer tab, click on the View Code.

Change togglebutton Color When Depressed in Excel

  • A code editor will open, in that code editor, 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

  • After entering the code, Click on Save.
  • Then press the code run command.
  • Doing this will run the code.
  • You will notice that the toggle button is here on the worksheet.

  • Now If you click on the toggle button, and hold the button, you will notice that the color is now changing as we click the toggle button.

  • Then if you depress the button, you will notice that the color changes again to another color.
  • Releasing the button will change the color in this case.

  • This is how we can change color while we depress the toggle button.

Example 3: Changing Toggle Button Color Based on Condition

This example lets you change the color of the toggle button when certain conditions are met.

Steps

  • First, go to the Developer tab and click on Insert If you don’t have that, you have to enable the Developer tab.
  • From the Activex Controls List, click on the Toggle Button.

Change togglebutton Color Based on logical Condition in Excel

  • Then place the toggle button by drawing them on the worksheet. The drawing area will determine the dimension of the ToggleButton.
  • Right-click on the Toggle Button and click on the View Code from the context menu.

Click View code to open vba editor

  • A code editor will open, in that code 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

  • We get to see that the toggle button is now formatted.

  • In the Number 1 value, we placed 10 and 16 as Number 2.
  • After that, we click the ToggleButton.
  • We observe that the color changes to yellow.

  • In the Number 1 value, we placed 16 and 16 as Number 2.
  • After that, we click the ToggleButton.
  • We observe that the color changes to Green.

  • In the Number 1 value, we placed 20 and 16 as Number 2.
  • After then we click the ToggleButton.
  • We observe that the color changes to Red.

toggle button color changed according to the logical caondition.


Example 4: Changing Toggle Button Color Based on Cell Value Type

This will enable the user to change the color of the toggle button if the cell value type changes.

Steps

  • First, go to the Developer tab and click on Insert. If you don’t have that, you have to enable the Developer tab.
  • From the Activex Controls List, click on the Toggle Button.
  • Then place the toggle button by drawing them on the worksheet. The drawing area will determine the dimension of the ToggleButton.
  • Click on the newly created toggle button and then go to the Developer tab again.
  • From the Developer tab, click on the View Code.

Changing toggol button Color Based on Cell Value Type in Excel

  • A code editor will open, in that code editor, 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

  • We will see that there is a new toggle button present in the worksheet.
  • On the worksheet, enter Random Text on cell B5.
  • And then press the toggle button.
  • The toggle button will turn green.

change color due to random text

  • On the worksheet, enter Number 15 on cell B5.
  • And then press the toggle button.
  • The toggle button will turn Red.

  • Then enter the date 4/12/22 on cell B5.
  • After this, press the toggle button.
  • The color of the toggle button will turn Purple.

change color for date input

  • Finally, delete everything from cell B5.
  • After this, press the toggle button.
  • The color of the toggle button will turn White.

toggle button color changed based on cell value type


Download Practice Workbook

Download this practice workbook below.


Conclusion

To sum it up, the issue of how we can change the color of the toggle button in Excel when pressed is answered here by four different examples. The VBA Macro method requires prior VBA-related knowledge to understand from scratch.

For this problem, a macro-enabled workbook is available to download where you can practice these methods.

Feel free to ask any questions or provide feedback through the comment section.

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