Generic Button Click event?

mark hansen

Well-known Member
Joined
Mar 6, 2006
Messages
534
Office Version
  1. 2016
Platform
  1. Windows
This is probably a silly question... But is there an event so when you click on a button, it takes information from the button (any button) and runs code. I have a user form with 80 buttons, and I have the same code on each Private sub CommandButtonX_Click() event … Except for the button number.

VBA Code:
Private Sub CommandButton78_Click()
    Dim Str As String
    Str = Me.CommandButton78.Caption
    Worksheets(Str).Activate
    If cbkAutoClose Then Call Label1_Click
End Sub

I was wondering if I could have something like … When a button is clicked... Take the caption from that button and activate that sheet and check to see if I need to close the user form. I see there is a mouse down and up events, but haven't used them...

When this user form is run, it takes the worksheet names and puts them on buttons, makes the user form Modeless so it just sits on top of the sheet while you work. (in a small display), When you click in the user form, it opens and displays buttons for each worksheet, allowing you to jump to a different worksheet. Its designed to be floating Table of Contents when you have many worksheets. Except for the .show command line, its completely self contained.

As I have (mostly) identical code for each buttons (up to 80), I have the above code replicated for each command button click event.

I'm just trying to make sure there isn't an easier way to set this up.

Thanks for any insight,
Mark
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Here is a demo file that shows how to do this with buttons on a userform. The strategy is to create a Class that handles events from all the buttons. On form initialization, you create an array using the class as the type, then loop through the buttons to add them to the array. The Class has the Click event handler. In this demo, you can see that it retrieves the caption from the clicked button.

 
Upvote 0
Thanks 6StringJazzer!
I was able to get your file... It works as I expected it to work... I've never used a class module, something I will need to explorer. This will certainty make my code a lot smaller.

Thanks for the examples.
Mark
 
Upvote 0
I can give you more explanation as needed. The Class is an object-oriented concept. VBA kinds of hints at being object-oriented (though it is not a full-blown OOL). A class is a kind of way of defining your own data type which also has custom operations defined for it and custom internal state data.
 
Upvote 0
I've put your code into my user form and it working well. One problem I running into, which I don't think has anything to do with classes, is in the BTN Class module, in the part of the code that activates the sheet you want, it has a line to basically resets the user form to pick up any changes in worksheet names. The line is:

If cbkAutoClose Then Call Label1_Click

The cbkAutoClose check box will allow the user to set if they want to keep the table of contents open (like if they want to view tab after tab), or close the table of contents to stay on the worksheet but get the TOC out of the way.

The code in the Class module is looking for the Lable1_Click code, which is inside the user form.
When I Rem the line out, it works, but, of course the TOC doesn't reduce in size.

Thoughts?

Thanks for your help and ideas.
 
Upvote 0
This is the code in the class module

VBA Code:
Option Explicit
Public WithEvents ButtonGroup As CommandButton
'
Private Sub ButtonGroup_Click()
    Dim cbkAutoClose As Boolean
    Dim Str As String
'    Dim Msg As String
'
'    Msg = "You clicked " & ButtonGroup.Name & vbCrLf & vbCrLf
'    Msg = Msg & "Caption: " & ButtonGroup.Caption & vbCrLf
'    Msg = Msg & "Left Position: " & ButtonGroup.Left & vbCrLf
'    Msg = Msg & "Top Position: " & ButtonGroup.Top
'    MsgBox Msg, vbInformation, ButtonGroup.Name
    Str = ButtonGroup.Caption
    Worksheets(Str).Activate
    If cbkAutoClose Then Call Label1_Click
End Sub
 
Upvote 0
I am not quite sure what your question is--but that line of code needs to qualify the userform.

Rich (BB code):
    If cbkAutoClose Then Call UserForm1.Label1_Click

Replace UserForm1 with the name of your actual userform.
 
Upvote 0
We all make those kinds of errors, and all you need is a fresh pair of eyes :)
 
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,613
Members
449,090
Latest member
vivek chauhan

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