Add toggle button in vba

ThomasOES

Board Regular
Joined
Aug 29, 2017
Messages
174
How to add a toggle button using vba. I need to populate a userform named "charform" with a variable number of toggle buttons. The cell "calbs" numerical value determines the quantity of toggle buttons. I would like the buttons (typically 8-10, but as many as 20) located vertically along the left side of the userform.

Thanks
Tom
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
There may be a way to create these dynamically, but I would take the approach of manually creating the max number of toggle buttons and only making the amount visible determined by your variable.
 
Upvote 0
VBA Code:
s = 100
r = 1
nextog = "Forms.ToggleButton" & r
For t = 1 To calbs

'charform.Controls.Add (nextog)

charform.Controls.Add ("Forms.togglebutton.1")

charform.ToggleButton1.Top = s
r = r + 1
s = s + 40
Next t

Hello

Any reason why code error for
VBA Code:
charform.Controls.Add (nextog)
?
 
Upvote 0
VBA Code:
s = 100
r = 1
nextog = "Forms.ToggleButton" & r
For t = 1 To calbs

'charform.Controls.Add (nextog)

charform.Controls.Add ("Forms.togglebutton.1")

charform.ToggleButton1.Top = s
r = r + 1
s = s + 40
Next t

Hello

Any reason why code error for
VBA Code:
charform.Controls.Add (nextog)
?
Found problem. Left a period out
Code:
nextog = "Forms.ToggleButton." & r
works
 
Upvote 0
Found problem. Left a period out
Code:
nextog = "Forms.ToggleButton." & r
works
Actually only works when r = 1.
So I try and name the toggle switch so I simply add all the ToggleSwitch1 I want and give each a different name.
Then there is an error message. "Cant set property at runtime"
VBA Code:
s = 100
r = 1

For t = 1 To calbs
nextog = "Forms.ToggleButton." & r

charform.Controls.Add ("Forms.togglebutton.1")
charform.ToggleButton1.Name = "togbut1"

r = r + 1
s = s + 40
Next t
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,334
Members
452,636
Latest member
laura12345

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