Worksheet_Deactivate Acting Strangely

jtfish

New Member
Joined
Sep 29, 2023
Messages
5
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I can't figure this out for the life of me. I have the following code in the worksheet object for a sheet called "Data":

Private Sub Worksheet_Deactivate()

If (ActiveSheet.AutoFilterMode And ActiveSheet.FilterMode) Or ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If

End Sub


This is supposed to clear all filters on the Data sheet as soon as the user leaves that sheet. The problem is, it does nothing to those filters and instead clears a filter on a different sheet every time. There are many sheets in the workbook and it doesn't matter which one I move to. One thing that is peculiar though, and I'm not sure if this is related, but I have another macro that unhides all sheets in the workbook and when that macro completes, the sheet that is erroneously being unfiltered is the one that is always active. It's not even the last sheet in the workbook. I apparently can't use "Worksheets("Data").Activate in the code above because then you're unable to leave the sheet. Also, I have a regular macro activated by a form control on the "Data" sheet that runs the same code above and that works fine, so I know the code works. Any ideas on how to resolve this?
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
have you tried:

VBA Code:
[I]If (Worksheets("Data").AutoFilterMode And Worksheets("Data").FilterMode) Or Worksheets("Data").FilterMode Then[/I]
Worksheets("Data")[I].ShowAllData[/I]
 
Upvote 0
Solution
Fluff: Your suggestion gives me a "Invalid use of Me keyword" error.

Candyman: That worked! I did need to get rid of the 's and 's though. I'm not sure if that was just a font translation thing.

Thank you both very much for for responding to my question. This forum is amazing. I used it extensively even before joining.
 
Upvote 0
This should not give that error
VBA Code:
Private Sub Worksheet_Deactivate()

If (Me.AutoFilterMode And Me.FilterMode) Or Me.FilterMode Then
Me.ShowAllData
End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,094
Messages
6,123,071
Members
449,092
Latest member
ipruravindra

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