Assigning hotkey to macro

Ironman

Well-known Member
Joined
Jan 31, 2004
Messages
871
Office Version
  1. 365
Platform
  1. Windows
Hi

The below code in a module runs when I click an icon on the toolbar:
VBA Code:
Sub FindDate()
Dim myDt As Date
Dim myInput As Variant
Dim CellFound As String

If ActiveSheet.Name <> "Training Log" And ActiveSheet.Name <> "Training 1981-1997" Then
    MsgBox "The Locate Entry Date function will only run in Training Log", vbInformation, "Function Invalid in This Sheet"
Exit Sub
    Else
End If

    On Error GoTo Error1
myInput = InputBox("Enter Date Below: (d/m/yy)", "Locate Training Log Entry Date")
    If myInput = "" Then
        MsgBox "Search cancelled!", vbInformation, "Locate Training Log Entry Date"
        Exit Sub
    Else
 
    End If
 
    If IsDate(myInput) Then
        myDt = myInput
        CellFound = Application.Match(CDbl(myDt), ActiveSheet.Range("A:A"), 0)
        ActiveSheet.Range("A" & CellFound).Activate
        Exit Sub
 
    Else
 
    End If
 
Error1:
MsgBox "Sorry, no Training Log entry found for that date", vbInformation, "Search Unsuccessful"

I want to assign a Control+\ keyboard shortcut to this instead and I've adapted the below code I've found online and inserted it in the Training Log sheet.
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.OnKey "^{RETURN}", "MyMacro"
End Sub

as follows:
VBA Code:
Application.OnKey "\^", "FindDate"
There's no error and I can't get it to run.

I'd really appreciate a solution.

Many thanks!
 
Last edited:

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

rlv01

Well-known Member
Joined
May 16, 2017
Messages
1,160
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Have you tried using the Alt-F8 macro dialog to assign it a hotkey?
 

Ironman

Well-known Member
Joined
Jan 31, 2004
Messages
871
Office Version
  1. 365
Platform
  1. Windows
Ahh, OK thank you. Yes, that works but Control + Shift + a letter is a bit cumbersome compared to clicking an icon. Is this the only combination I'm able to use to do this?
 

rlv01

Well-known Member
Joined
May 16, 2017
Messages
1,160
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
If you are after CTRL + backslash, try putting the caret before the backslash.

VBA Code:
Application.OnKey "^\", "FindDate"
 
Solution

Ironman

Well-known Member
Joined
Jan 31, 2004
Messages
871
Office Version
  1. 365
Platform
  1. Windows
Thanks ever so much, that works! I should have known to put the carrot before the stick :biggrin:
 

Forum statistics

Threads
1,148,393
Messages
5,746,438
Members
424,019
Latest member
dpteo

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