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.
Download Practice Workbook
Download this practice workbook below.
4 Suitable Examples to Change Toggle Button Color When Pressed in Excel
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 of the Excel.
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.
- Then place the toggle button by drawing them on the worksheet. The drawing area will determine the dimension of the toggle button.
- 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.
Example 2: Changing Toggle Button Color When Depressed
Compared to the previous example, this example will let you able to 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.
- 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.
Read More: How to Add Button in Excel (3 Handy Ways)
Similar Readings
- How to Use VBA Code for Submit Button in Excel (with Easy Steps)
- How to Clear Cells in Excel with Button (with Detailed Steps)
- Excel Button to Print Specific Sheets (With Easy Steps)
- How to Hide Columns with Button in Excel (4 Suitable Methods)
- VBA Code for Print Button in Excel (5 Examples)
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.
- 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.
- 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 then 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 then 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.
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.
- 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.
- 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.
- Finally, delete everything from cell B5.
- After this, press the toggle button.
- The color of the toggle button will turn White.
Read More: How to Change Cell Value Using Toggle Button in Excel
Conclusion
To sum it up, the issue of how we can change the color of the toggle 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 feedback through the comment section. Any suggestion for the betterment of the Exceldemy community will be highly appreciable
Related Articles
- How to Set Option Button Value in Excel VBA (4 Easy Steps)
- Insert Excel VBA Radio Button Input Box (3 Easy Methods)
- How to Remove a Form Control in Excel (3 Quick Ways)
- Add Radio Buttons in Excel (2 Effective Ways)
- How to Group Radio Buttons in Excel (2 Simple Methods)
- How to Create & Apply Option Button Click Event in Excel VBA