application.OnKey not working as Add-in

as0921

New Member
Joined
Dec 13, 2016
Messages
6
the application.onkey worked before creating it as an add-in now the only way the command will work is if I go into VBA project screen and run the sub. How do I get it to run without running the a sub in VBA projects.


--------- code "this workbook"
Option Explicit
Public Sub Workbook_Activate()
Application.OnKey "^+s", "Autofilter"
End Sub

Public Sub Workbook_Deactivate()
Application.OnKey "^+s"
End Sub

-------- code "Module 1"
Option Explicit
Public Sub Autofilter()
Dim FinalRow As Long
Dim FinalColumn As Long
Dim PRange As Range
Application.OnKey "^+s", "Autofilter"
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
Range("A" & FinalRow).Autofilter
End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try placing the OnKey code in the addin workbook Open event instead of the Activate event but then the key will remain hooked throughout

Are you trying to activate the key only when a specific workbook is active ? if so then you will have to hook the Application events upon loading the adding via the use of the WithEvents Keyword.
 
Upvote 0
Yes, want this shortcut work on any workbook that is open. With your suggestion I change activate to Open and it did not do anything.

Option Explicit
Public Sub Workbook_open()
Application.OnKey "^+s", "Autofilter"
End Sub

Public Sub Workbook_Deactive()
Application.OnKey "^+s"
End Sub

----- Module

Option Explicit
Public Sub Autofilter()
Dim FinalRow As Long
Dim FinalColumn As Long
Dim PRange As Range
WSD.Application.OnKey "^+s", "Autofilter"
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
Range("A" & FinalRow).Autofilter
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,942
Messages
6,122,366
Members
449,080
Latest member
Armadillos

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