Trigger VBA on double click

kgkev

Well-known Member
Joined
Jun 24, 2008
Messages
1,291
Office Version
  1. 365
Platform
  1. Windows
Couple of questions.

I want to apply an advanced filter based on the cell that is double clicked.


This works

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Global_Filter
End Sub


I have a few questions

1. The Sheets "Data" is deleted and recreated as part of previous VBA routine, therefore the beforedoubleclick event is removed.
Can I make an beforedoubleclick event for the whole workbook?

2. After the double click, the cell that was clicked is still selected for edit. How can I cancel this selection?

3. I want to clear the filter by pressing ESC on the keyboard. Is it possible to trigger an event from an ESC key press?
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
You can use a workbook event like
Code:
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
   If Sh.Name <> "Data" Then Exit Sub
   Cancel = True
   Global_Filter
End Sub
There is no trigger for Esc.
And whilst it is possible to use the Esc key to run a macro, you will loose the normal functionality of that key.
You would probably be better of just clicking "Clear" on the Data tab.
 
Upvote 0
Solution
Another option would be to doubleclick on a certain cell to clear the filter like
Code:
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
   If Sh.Name <> "Data" Then Exit Sub
   Cancel = True
   If Not Intersect(Target, Sh.Range("A1:F1")) Is Nothing Then
      Global_Filter
   ElseIf Target.Address(0, 0) = "J1" Then
      Sh.ShowAllData
   End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,160
Members
453,021
Latest member
Justyna P

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