Many buttons on user form

diddi

Well-known Member
Joined
May 20, 2004
Messages
3,337
Office Version
  1. 2010
Platform
  1. Windows
I have a userform with more than 50 buttons which all access basically the same code. How can I detect a button_click event, determine which button was clicked and impliment one piece of code for all buttons? I think I need to use a Class module but not sure and cant find any good resources online.

thx
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Welcome to the Board!

Why not just have your code in a general module, so it's available to the whole VB Project? e.g.

<font face=Calibri><SPAN style="color:#00007F">Public</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br><br><SPAN style="color:#00007F">Sub</SPAN> foo()<br>    MsgBox "You clicked Button #" & i<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

If you need to know the button # to use in the code, you can delare a public variable and set it in each button.

<font face=Calibri><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> CommandButton1_Click()<br>    i = 1<br>    Application.Run "Foo"<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

How about posting your global code (the one piece you want to reference) so we can see exactly what you're trying to do?

Hope that helps,

Smitty
 
Upvote 0
yeh this is how i started, but you end up with something like this...

sub commandbutton1_click
do something
end sub

sub commandbutton2_click
do something
end sub

sub commandbutton3_click
do something
end sub

....
sub commandbutton66_click
do something
end sub

there's got to be a more elegant way, surely?
 
Upvote 0
Why do you have 50 command buttons?
 
Upvote 0
The way you mention in the OP is to insert a class module. Put this code in the class module.
Code:
Public WithEvents allButtons As msforms.CommandButton

Private Sub allButtons_Click()
    MsgBox "You clicked"
End Sub
Then, this code in the userform's code module will assign each of the command buttons, other than the Close button (CommandButton1), as a property of a member of the new class and clicking a button on the userform will trigger Click events in both the Userform code module and the new Class module.

Code:
Dim myButtons() As New Class1

Private Sub UserForm_Initialize()
    Dim i As Long
    With Me
        ReDim myButtons(0 To .Controls.Count - 1)
        For i = 0 To .Controls.Count - 1
            If Left(.Controls(i).Name, 13) = "CommandButton" Then
                If .Controls(i).Name <> "CommandButton1" Then
                Rem exclude Close button
                    Set myButtons(i).allButtons = Me.Controls(i)
                End If
            End If
        Next i
    End With
End Sub
 
Upvote 0
ah thx
this is just the ticket. and better still, i understand what you have done so i can use the method again rather than bother others.


the application is a map of storage sheds, each shed is a button which activates the accounts management etc for that particular shed. same code different shed number.
 
Upvote 0
I glad my code helped, but I have to agree that 50 command buttons that call the same routine is excesive. Rather than button1, button2,...,button50, it might be easier if there was one textbox/cell/something that held a number between 1 and 50 and one command button.
 
Upvote 0
I second Mike, well I second him seconding me if that makes any sense.:)

Couldn't you list the shed numbers in a control like a listbox or combobox?
 
Upvote 0
the purpose of so many buttons rather than a combobox is to provide an interactive map for the user. i like to make things simple at the front end even if it costs me extra effort in logistics. i must say that it has come up really well and will be expanded to nearly 200 storage spaces in about 8 months time. thx again for your input.
 
Upvote 0

Forum statistics

Threads
1,216,622
Messages
6,131,777
Members
449,671
Latest member
OALes

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