Multiple Toggle Buttons VBA Code

IannW

New Member
Joined
Aug 25, 2018
Messages
18
Hi All

I have a spreadsheet with a row of Toggle Buttons. The toggle buttons are to indicate if a task has been completed. When clicking the button it says 'YES' and using conditional formatting, turns the whole row green.

I have some VBA attached to the toggle button to determine the colour when true of false, see below:

Private Sub ToggleButton1_Click()
If ToggleButton1.Value = True Then
ToggleButton1.BackColor = RGB(173, 219, 123)
ToggleButton1.Caption = "Yes"
Else


ToggleButton1.BackColor = RGB(255, 204, 204)
ToggleButton1.Caption = "No"
End If

End Sub

My question is this. When I add another toggle button, I copy the same code into VBA but have to edit the toggle button name to ToggleButton2, but as you can see, this has to be done 6 times in the code. I want to keep adding to the list of tasks but it takes ages to edit the code each time.

Is there a way to attach the VBA code to a new toggle button and have the name automatically increment each time?

Help gratefully received.
 

Some videos you may like

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

IannW

New Member
Joined
Aug 25, 2018
Messages
18
I like this option. Can you specify a cell range for this, as when I tried it (slightly different code) it worked on every cell in the worksheet instead of just the cells I wanted. Also, can you conditional format a cell to display text when the cell is a certain colour? I can do the other way round easy enough.

Regards
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
16,987
Office Version
  1. 2013
Platform
  1. Windows
You said:
I like this option.

Which Option are you referring to there are several here.

My script restricts the code to column A
 

Watch MrExcel Video

Forum statistics

Threads
1,113,838
Messages
5,544,606
Members
410,626
Latest member
rkmadasu
Top