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.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
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
 
Upvote 0
You said:
I like this option.

Which Option are you referring to there are several here.

My script restricts the code to column A
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,313
Members
452,634
Latest member
cpostell

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top