Macro keyboard shortcut doesn't work

Yamezz

Active Member
Joined
Nov 22, 2006
Messages
364
Office Version
  1. 2019
I have a couple of macros in my PERSONAL.XLSB file. Both are assigned a keyboard shortcut, but only one works with the shortcut. It worked briefly, but not anymore.
This works:
VBA Code:
Sub ShowAllCTRLEND()
'
' Keyboard Shortcut: Ctrl+q
'
    If ActiveSheet.FilterMode = True Then
    
    ActiveSheet.AutoFilter.ShowAllData
    Cells.Find(What:="*", After:=[A1], SearchDirection:=xlPrevious).Select
    Cells(ActiveCell.Row, 1).Select
    ActiveCell.Offset(1, 3).Activate
    
    Else
    Cells.Find(What:="*", After:=[A1], SearchDirection:=xlPrevious).Select
    Cells(ActiveCell.Row, 1).Select
    ActiveCell.Offset(1, 3).Activate
    End If

End Sub

This code will run fine if evoked manually, but will not run with the shortcut, despite being listed as a shortcut in the 'options' of the macro menu.
VBA Code:
Sub AlignRight()
'
' AlignRight Macro
'
' Keyboard Shortcut: Ctrl+Shift+R
'
    With Selection
        .HorizontalAlignment = xlRight
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
End Sub

What could be the cause?
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Perhaps you have another macro using that shortcut?
 
Upvote 0
Do you have any add-ins loaded? Does anything happen when you press the shortcut keys?
 
Upvote 0
No add-ins.
When I press CTRL + q, my ShowAllCTRLEND() macro runs fine.
When I press CTRL + SHIFT + R, nothing happens at all.
 
Upvote 0
If you've moved that macro to another code module its shortcut will not stick.
To "restore" the assignment of the shortcut keys the OnKey method can be used.

VBA Code:
Public Sub Yamezz()

    Const KEYALT   As String = "%"
    Const KEYCTRL  As String = "^"
    Const KEYSHIFT As String = "+"

    Excel.Application.OnKey KEYSHIFT & KEYCTRL & "r", "AlignRight"
End Sub
 
Upvote 0
I copied the code and ran it. Now when I press CTRL + SHIFT + R I receive the error:
Cannot run the macro "C:\Users\James\AppData\Roaming\Microsoft\Excel\XLSTART\PERSONAL.XLSB'!AlignRight'. The macro may not be available in this workbook or all macros may be disabled.
But when I press CTRL + q, my ShowAllCTRLEND() macro runs fine.
 
Upvote 0
Which workbook and module is the problem code in?
 
Upvote 0
I'm not able to reproduce your issue, so not sure what's going on.
To begin with, move your macro (cut & paste) forth and back between different modules in your PERSONAL.XLSB workbook. That way we're sure the connection between shortcut keys and macro is lost. Now save the PERSONAL.XLSB, quit Excel, restart Excel and open a new workbook. At this point there are two things you can do in the context of a possible solution to your problem.

1) In Excel press Alt F8, choose your macro (within PERSONAL.XLSB), press Options button and reassign the desired shortcut keys. Quit Excel and save PERSONAL.XLSB when you're prompted to do so. This should work. If it doesn't then opt for 2).

2) Every time Excel is started and PERSONAL.XLSB is automatically opened, run a macro that takes care of the shortcut assignment. In this case:
This goes in the ThisWorkbook module of PERSONAL.XLSB:
VBA Code:
Private Sub Workbook_Open()
    Yamezz
End Sub

This goes in a standard module of PERSONAL.XLSB:
Note that the assignment consists of the name of the code module in which the desired macro is located. Change this name according to your situation.
VBA Code:
Public Sub AlignRight()
'
' AlignRight Macro
'
' Keyboard Shortcut: Ctrl+Shift+R
'
    With Selection
        .HorizontalAlignment = xlRight
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
End Sub

Public Sub Yamezz()

    Const KEYALT   As String = "%"
    Const KEYCTRL  As String = "^"
    Const KEYSHIFT As String = "+"

    Excel.Application.OnKey KEYSHIFT & KEYCTRL & "R", "'PERSONAL.XLSB'!Module1.AlignRight"  ' << change Module name accordingly
End Sub
 
Upvote 0
Method 1 did not work.
I tried something a little different though, that seems to have stuck.
Instead of my macros being in individual modules, I created a module named "Custom", then cut and pasted my handful of little macros all into the "Custom" module and reassigned the keyboard shortcuts. It all appears to be working so far.

Thanks for the help guys.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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