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

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
9,158
Office Version
  1. 2016
Platform
  1. Windows
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

as0921

New Member
Joined
Dec 13, 2016
Messages
6
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,191,373
Messages
5,986,271
Members
440,015
Latest member
knijgh

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