Naming Command Button Sub With Variable Name

Coach3131

Board Regular
Joined
Nov 23, 2007
Messages
242
I have a 30 command buttons that all do the same thing

So I would like to make it to where if you click CommandButton1 it relates to File1 and so forth.... it allows a File to be associated with the 1 group

Is there a way to do that with 1 Sub routine or do I need a sub for each button?
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
This might be a stupid question but why do you have 30 command buttons.:eek:

What is the same thing they are doing?
 
Upvote 0
If they are buttons from the Control Toolbox then you have to use the Click event of each but you can call another macro from the click event and pass a different argument from each button. If they are from the Forms toolbar then you can assign the same macro but with a different argument to each button.
 
Upvote 0
It would probably be easier to have a list/combobox and one button!
 
Upvote 0
Because I want to associate a file for each unit

Unit1 = File1

Sub CommandUnit1_Click allows the user to pick where the file (File1) is to associate to Unit1

Didn't know if I should just copy that routine 30 times or if there was a way to cut down on the amount of code
 
Upvote 0
Put the routine into a separate sub and call it from each Click event.
 
Upvote 0
In order to do that I need to link the number in 1 as a variable since it is used 7 times

Sub CommandUnit1_Click

Unit1
File1
Range1

etc. are references made within this file
 
Upvote 0
You should create a configuration table in a spreadsheet and then 'LINK' the properties to your commandbuttons by way of the tag property. That way, if you need to add, remove, or change settings, you will not have to edit your code, but only the spreadsheet. The tag property can then be set with the address of the buttons config settings. When the button is clicked, the range is looked up and all of the properties are available to you. Do you understand? Send me your workbook and I'll show you what I mean.

tom@jantomsolutions.com
 
Upvote 0

Forum statistics

Threads
1,213,510
Messages
6,114,037
Members
448,543
Latest member
MartinLarkin

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