Shortcut to "click" a commandbutton

UHsoccer

Well-known Member
Joined
Apr 3, 2002
Messages
1,023
I have a shortcut (Ctrl-M) that changes the display to a sheet called "Select". In addtion I would also like to have it click a commandbutton that is on the displayed sheet.
When I record a macro, it does nor record anything when I click on a commandbutton.

It is an action that is performed many, many times per day and it would reduce the number of clicks a user would have to perform.

Here is the workbook code
Code:
Private Sub Workbook_Open()
     Application.OnKey "^m", "Display_Select"  '<- Ctrl + m
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
If you want to run the code behind the button why not put it in a standard module.

Then you can just call it from the code that changes the display and it can still be called when the button is clicked.
 
Upvote 0
wouldnt you just call the commandbutton?


Code:
Private Sub CommandButton1_Click()

End Sub


so this is commandbutton1_click

can't u just call that?
 
Upvote 0
You'll have to create a macro that calls both Display_Select and the sub that the button calls(right click>assign macro will let you see it's name) then set that macro to the shortcut
 
Upvote 0
I tried the option from Quiet Riot, but to no avail.

Here is the code in "This Workbook"
Code:
Private Sub Workbook_Open()
     Application.OnKey "^m", "Display_Select"  '<- Ctrl + m
     
     CommandButton1_Click
End Sub
 
Upvote 0
Just my 2 cents, but I'd take Norie's advice and put your code in a general module, then call it in your Open and Click events.

That's a lot easier than trying to fuddle with getting the click event to work as you envision. ;)

HTH,

Smitty
 
Upvote 0
I have a shortcut (Ctrl-M) that changes the display to a sheet called "Select". In addtion I would also like to have it click a commandbutton that is on the displayed sheet.


Just do this:

First, presumably your workbook Open event is this:


Code:
Private Sub Workbook_Open()
Application.OnKey "^m", "Display_Select"  '<- Ctrl + m
End Sub

That means you have, or should have, this for your Close or Deactivate event:

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnKey "^m"
End Sub



Now, you never said what the name of your command button is on the Select sheet, but let's assume that it is named CommandButton1 and is, by inference, created from the control toolbox.

Imagine that this is your click event for that button in its worksheet module:

Code:
Public Sub CommandButton1_Click()
MsgBox "Hello"
End Sub

Important, notice that you must make this a Public statement just like i have it here.


Then, you have a sub named "Display_Select" in a standard module:

Code:
Sub Display_Select()
Sheets("Select").Activate
Call Worksheets("Select").CommandButton1_Click
End Sub


And that's it, relatively painless, works for me.
 
Upvote 0
The solution from Tom Urtis works as esxpeted. The code is simple and the statement to make the "Sub CommandButton1_click" Public, made the difference

Thanks for the help
 
Upvote 0
A follow-up tip, if you are on a sheet other than the Select sheet when you press Ctrl+M, and then while you are on the Select sheet you press Ctrl+M again and nothing seems to happen, it will be because you need to do a one-time adjustment of changing the TakeFocusOnClick property of the Command Button to False, if you had not already done so.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,693
Members
448,979
Latest member
DET4492

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