Userform - run same code on any button click

liampog

Active Member
Joined
Aug 3, 2010
Messages
316
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi

I have a number of Command Buttons with different captions.

When the user clicks a command button, I want the code to change the activecell value to the caption of the button (the userform is called by double-clicking a single cell).

I don't want to create code for each command button but not sure if this is possible.

Thanks
Liam
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
If the buttons are on the sheet (not on userform) you can assign the same macro to all the buttons:

VBA Code:
Sub Button_Click()
ActiveCell.Value = ActiveSheet.Buttons(Application.Caller).Name
End Sub


If you want to save manual work and add this macro to all buttons in a sheet you may use such code to do it:
VBA Code:
Sub Assign_macro_to_all_Btns()
Dim btn As Object
For Each btn In ActiveSheet.Buttons
  btn.OnAction = "Button_Click"
Next btn
End Sub
Of course you may use some filtering in the above procedure to add macro only to soeme of them 8-)
 
Upvote 0
Thanks Kaper

The buttons are on the userform unfortunately (and need to be in order for them not to be visible all the time).

Basically, when the user double-clicks on a cell, the userform appears.

When they click any button on the userform, the caption for that button is input into the ActiveCell and then the Userform unloads itself (to disappear).

There are 57 buttons on the userform so I'm looking for the simplest way rather than having to add code to each button's "Click"
 
Upvote 0
You need to use a class. Searching this forum should provide several examples. ;)
 
Upvote 0
Or second result for such a bit longer search term:
same code for several commandbuttons on userform

https://stackoverflow.com/questions/69612204/unique-code-for-several-commandbuttons-in-userform

In my opinion this version almost perfectly fits your situation . The only change will be the output to active cell, not to always the same E5:
VBA Code:
Private Sub CmdGroup_Click()
ActiveCell.Value = CmdGroup.Caption
End Sub
If you are not familiar with classes: note class module name, which has to be changed from standard one (Class1) - the rest shall be working smoothly
 
Upvote 0
Thanks all for your help. Have researched classes in userforms and have been able to figure it out so it works 👍
 
Upvote 0

Forum statistics

Threads
1,224,551
Messages
6,179,473
Members
452,915
Latest member
hannnahheileen

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