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

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,358
Office Version
  1. 365
Platform
  1. Windows
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

QuietRiot

Well-known Member
Joined
May 18, 2007
Messages
1,079
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
  2. MacOS
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

Fr33dan

New Member
Joined
Jul 18, 2007
Messages
19
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

UHsoccer

Well-known Member
Joined
Apr 3, 2002
Messages
1,023
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

Smitty

Legend
Joined
May 15, 2003
Messages
29,536
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

Tom Urtis

MrExcel MVP
Joined
Feb 10, 2002
Messages
11,283
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

UHsoccer

Well-known Member
Joined
Apr 3, 2002
Messages
1,023
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

Tom Urtis

MrExcel MVP
Joined
Feb 10, 2002
Messages
11,283
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,191,576
Messages
5,987,390
Members
440,095
Latest member
yanaungmyint

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
Top