Userform command button

WAF875

New Member
Joined
Apr 17, 2016
Messages
24
Is it possible to assign a variable number to command buttons I am using in a userform. If for example I say CommandButton1.BackColor = 255 I would like that to be CommandButton (Range("A1")).BackColor = 255 in which the command button number would be the value in cell A1. Thanks Hope someone can help
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
You need the controls collection of the userform. If the code is in the form:

VBA Code:
me.controls("Commandbutton" & range("A1").value).backcolor = 255
 
Upvote 0
Solution
You need the controls collection of the userform. If the code is in the form:

VBA Code:
me.controls("Commandbutton" & range("A1").value).backcolor = 255
Thanks RoryA that worked fine, I was trying to do it outside the Private subs
 
Upvote 0
If the code is not in the userform, you'd use userformname.controls (or use the relevant variable holding your form instance if you're doing things "properly" ;))
 
Upvote 0
If the code is not in the userform, you'd use userformname.controls (or use the relevant variable holding your form instance if you're doing things "properly" ;))
Thanks again that would make things easier
 
Upvote 0
Thanks RoryA that worked fine, I was trying to do it outside the Private subs
The marked solution has been changed accordingly.
In your future questions, please mark the post as the solution that actually answered your question, instead of your feedback message as it will help future readers. No further action is required for this thread.
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,823
Members
449,049
Latest member
cybersurfer5000

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