application.OnKey for autofilter

as0921

New Member
Joined
Dec 13, 2016
Messages
6
I am trying to create keyboard shortcuts and I am having zero luck. This is what I have so far

Option Explicit
Private Sub workbook_active()
Application.OnKey "^+{S}", "Selection.AutoFilter"
End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Code:
Option Explicit
Private Sub workbook_[B]activate[/B]()
Application.OnKey "^+{S}", "AFilter"
End sub

Private Sub Workbook_Deactivate()
Application.Onkey "^+{S}
End Sub

Paste this into "This Workbook"

Code:
Sub AFilter ()
Selection.AutoFilter
End Sub
Then make a new Module and paste this code in there.

I'd personally use something like Range("A1:F1").AutoFilter instead of Selection.AutoFilter if you know what range you'll be applying autofilter to.

hope this helps!
 
Last edited:
Upvote 0
Thanks Tim,

So my goal is to share this with my team members. At my previous job someone already built excel keyboard shortcuts. could I use:

DIM FinalCOL As Long
 
Upvote 0
I am not quite sure what you're trying to say. Do you want the code to look up the last column with a value and then apply autofilter all those columns?
 
Upvote 0
Morning Tim, Yes I would like the code to auto filter all the columns that have a header. I ran the following for test and it worked but only when I clicked run macro.

Sub Call_it()
Application.OnKey "^+s", "Testing"
End Sub

Sub Testing()
MsgBox "This is a testing message "
End Sub
 
Upvote 0
The reason that the code only runs when you run the macro is because the macro isn't always active. When you want a macro to keep checking if the keys are being pressed you'd need
Code:
Sub  Workbook_Activate()
or
Code:
Sub Worksheet_Change()
each time something in the worksheet changes. Make sure to use the last mentioned sub in the preferred Worksheet tab in VBA.

This code should apply autofilter to all columns with values in their headers
Code:
Sub LastColumnAFilter()
LC = Sheets(1).Range("A" & Rows.Count).End(xlUp).Row
Range("A1:A" & LC).AutoFilter

End Sub
 
Upvote 0
Thanks Tim,

So this is what I have so far in the Workbook

Option Explicit

Private Sub Workbook_Activate()
Application.OnKey "^+s", "AFilter"
End Sub

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

Sub AFilter()
Selection.AutoFilter
End Sub

This is what I have in the Module:

Option Explicit
Private Sub Workbook_Activate()
Dim FinalCol As Long
Dim FinalRow As Long
Application.OnKey "^+{S}", "AFilter"
End Sub
Private Sub Workbook_Deactivate()
Dim FinalCol As Long
Dim FinalRow As Long
Application.OnKey "^+{S}"
End Sub

Sub LastcolumnAFilter()
Dim FinalCol As Long
Dim FinalRow As Long
FinalCol = Cells(Rows.Count, 1).End(x1up).Col
Range("A1:A" & FinalCol).AutoFilter
End Sub

Aren't I worried about columns not Rows?
 
Upvote 0

Forum statistics

Threads
1,213,549
Messages
6,114,264
Members
448,558
Latest member
aivin

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