[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 493
  • Last Modified:

How to remove filters on exit

Dear experts,

My worksheet contains some filters in some fields. Is there a way to remove these fields from a VB code on exit (when the workbook is closed) ?

Thanks

currentdb
0
currentdb
Asked:
currentdb
  • 23
  • 23
  • 5
  • +1
2 Solutions
 
Ardhendu SarangiSr. Project ManagerCommented:
Hi,
You can use the following code to remove the autofilter on exit.


Private Sub Workbook_BeforeClose(Cancel As Boolean)
    ActiveSheet.ShowAllData
    Selection.AutoFilter
End Sub

Open in new window

0
 
SiddharthRoutCommented:
Try this

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    '~~> Check for filter, turn off if exists
    If ActiveSheet.AutoFilterMode Then
        Sheets("Sheet1").AutoFilter
    End If
End Sub

Open in new window


Sid
0
 
SiddharthRoutCommented:
crossover.

Sid
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
SiddharthRoutCommented:
To remove autofilter from every sheet in the workbook, do this.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim ws As Worksheet
    
    For Each ws In ActiveWorkbook.Sheets
        '~~> Check for filter, turn off if exists
        If ws.AutoFilterMode Then ws.AutoFilter
    Next
End Sub

Open in new window


Sid
0
 
currentdbAuthor Commented:
one worksheet is enough for now, but I'll remember your code if there are several filter on more than one worksheet. Lemme try both codes, yours and the one from pari123.
0
 
currentdbAuthor Commented:
I'm back. Tried both, but no effect. I closed the workbook and when I reopened it, the filter was still there.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
'~~> Check for filter, turn off if exists
    If ActiveSheet.AutoFilterMode Then
        Sheets("QUERY_FOR_GSL2").AutoFilter
    End If
End Sub

Open in new window


Maybe the code needs to be after the one that is formatting the AY column ?
0
 
SiddharthRoutCommented:
Did you save it before closing ?

Sid
0
 
currentdbAuthor Commented:
Now I have the first compile error. It was not there when I re-opened the workbook. But now as I wanted to close it again, a 438 copile error appeared ''Property or method not generated by this object''
0
 
currentdbAuthor Commented:
Did you save it before closing ? --> Yes I did

0
 
SiddharthRoutCommented:
May I see the workbook?

Sid
0
 
currentdbAuthor Commented:
It just highlights the line 4
0
 
SiddharthRoutCommented:
Try this

Private Sub Workbook_BeforeClose(Cancel As Boolean)
'~~> Check for filter, turn off if exists
    If Sheets("QUERY_FOR_GSL2").AutoFilterMode Then
        Sheets("QUERY_FOR_GSL2").AutoFilter
    End If
End Sub

Open in new window


Sid
0
 
Rory ArchibaldCommented:
Surely:
Private Sub Workbook_BeforeClose(Cancel As Boolean)  
'~~> Check for filter, turn off if exists  
    If Sheets("QUERY_FOR_GSL2").AutoFilterMode Then  
        Sheets("QUERY_FOR_GSL2").ShowAllData
    End If  
End Sub

Open in new window

0
 
Ardhendu SarangiSr. Project ManagerCommented:
Can you tell me where did you paste the code?  you need to save it on the Thisworkbook page and not in a module.


Private Sub Workbook_BeforeClose(Cancel As Boolean)
    ActiveSheet.ShowAllData
    Selection.AutoFilter
    ThisWorkbook.Save
End Sub

Open in new window

0
 
SiddharthRoutCommented:
If I am not wrong,

.ShowAllData

doesn't turn off the autofilter. It just shows all data :)

Sid
0
 
Ardhendu SarangiSr. Project ManagerCommented:
Hi,
Can you also mention what is your version of Excel?
0
 
Ardhendu SarangiSr. Project ManagerCommented:
==> If I am not wrong,

.ShowAllData

doesn't turn off the autofilter. It just shows all data :)


you are right...
0
 
currentdbAuthor Commented:
Can you tell me where did you paste the code?  you need to save it on the Thisworkbook page and not in a module. --> It was saved in ThisWorkbook page. Saving it into a module would mean execute it manually...

Sid, I'm going to try the code you suggested at post ID 35199562 before posting a sample of the workbook.
0
 
SiddharthRoutCommented:
>>>Sid, I'm going to try the code you suggested at post ID 35199562 before posting a sample of the workbook.

and save the workbook manually.

A Suggestion: Do not include

ThisWorkbook.Save

in the before close event

as it would save every time you close the workbook even when you don't want to save the changes.

Sid
0
 
Ardhendu SarangiSr. Project ManagerCommented:
Can you also try the following once too -


d
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    ActiveSheet.ShowAllData
    Selection.AutoFilter
    ThisWorkbook.Save
End Sub

Open in new window

0
 
Rory ArchibaldCommented:
It was not clear whether the asker wanted the filter completely removed, or just all data visible.

Yours won't work since the Autofilter property of the worksheet simply returns an Autofilter object.

You could use:
Private Sub Workbook_BeforeClose(Cancel As Boolean)    
'~~> Check for filter, turn off if exists    
    If Sheets("QUERY_FOR_GSL2").AutoFilterMode Then    
        Sheets("QUERY_FOR_GSL2").AutoFilterMode = False
    End If    
End Sub

Open in new window


if you actually want to remove the filter completely.
0
 
currentdbAuthor Commented:
pari: Can you also mention what is your version of Excel? --> Excel 2007
0
 
Rory ArchibaldCommented:
If it's a table on the sheet then something like:
Private Sub Workbook_BeforeClose(Cancel As Boolean)      
'~~> Check for filter, turn off if exists      
    Sheets("QUERY_FOR_GSL2").Listobjects(1).Autofilter.ShowAllData
End Sub

Open in new window

to simply show all the data, or:
Private Sub Workbook_BeforeClose(Cancel As Boolean)      
'~~> Check for filter, turn off if exists      
    Sheets("QUERY_FOR_GSL2").Listobjects(1).ShowAutofilter
End Sub

Open in new window


to remove it.
0
 
SiddharthRoutCommented:
I just realised what Rory said is correct however it still won't take off the autofilter.

This is tried and tested.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    '~~> Check for filter, turn off if exists
    If Sheets("QUERY_FOR_GSL2").AutoFilterMode Then
        Sheets("QUERY_FOR_GSL2").AutoFilterMode = False
        ret = MsgBox("Do you want to save this workbook", vbOKCancel)
        If ret = vbOK Then ThisWorkbook.Save
    End If
End Sub

Open in new window


Sid
0
 
currentdbAuthor Commented:
rorya: your code seems to work, but before inserting it, my computer popped me with these messages ''Not enough memory''. Weird. And I don't even have many opened apps, just Excel and the workbook is very light.

The other problem that appeared is that now it asks me to save the workbook even if I already clicked on the ''Save'' button at the top.

Sid just suggested that I don't have to include the ThisWorkbook.Save line. It's not included, but then why Excel asks me to save the workbook for a second time if I already saved it ?

0
 
SiddharthRoutCommented:
See the code that I posted :)

Sid
0
 
currentdbAuthor Commented:
Sid,

You have not defined the ''ret'' variable as it returns a compile error.
0
 
SiddharthRoutCommented:
Sorry, Add the at the top of the code

Dim Ret

Sid
0
 
currentdbAuthor Commented:
The workbook just crashed for a nineth time in a row. Seems like something in the code eats lots of system memory. Maybe that causes the ''Insufficient memory'' message to appear.
0
 
Rory ArchibaldCommented:
If you save before closing, then the closing code changes the filters, so you need to save that change too. Sid's code for the Save part takes care of that.
I can't really help with errors that occurred before you added my code. ;)
0
 
SiddharthRoutCommented:
Can u upload the file?

Sid
0
 
currentdbAuthor Commented:
rorya: I can't really help with errors that occurred before you added my code. --> These errors were not caused by your code, not even by Sid's code, but I have no real idea. Maybe this computer just play devil games with me :)

sid: here's the file
userform-sample-v7b-5.xlsm
0
 
SiddharthRoutCommented:
It works perfectly for me.

Sid
0
 
SiddharthRoutCommented:
Restart your pc and then try :)

Sid
0
 
currentdbAuthor Commented:
Sid, yes it works, but is that necessary to click 2 times when you want to save ? First Excel itself asks you to save, and after, it's the message box from the Private Sub Workbook_BeforeClose event. Does that mean there is no possiblity just to use one confirmation message instead of two ?
0
 
SiddharthRoutCommented:
Hmmm Strange

I get only one mesage box.

Sid
0
 
currentdbAuthor Commented:
I tested the code back on the other sheet that is linked to a Ms-Access table, but I'm puzzled that the code does not have any effect.

What I can notice about this sheet that is linked this way is that all rows are already using a filter by default. Tried to shut it off manually, but it does not work.
Now the columns that the filter are columns O and AY. And that means a double filter...first filter from Access then second from Excel. If removing the Excel filter, data will be as it is, not filtered even if the Access filter is still on each column.

I don't know if there's some solution for this or if it has to be done manually every time.
0
 
currentdbAuthor Commented:
Hmmm Strange
I get only one mesage box.  --> If you don't click at all on this small disk at the top of Excel, just close, really close the worksheet, then first the Excel system message appears...you click ''yes''...and second the message box, but by the time you click ''yes'', the filter was already removed.  

0
 
SiddharthRoutCommented:
Nope. I still get only 1 message box.

Also the file uploaded, is that the original file? I mean I don't see any connections to Access?

Sid
0
 
currentdbAuthor Commented:
Also the file uploaded, is that the original file? I mean I don't see any connections to Access? --> It's just a sample file as the original file is much bigger in size. But if you have Access, I can post you a sample Access mdb with the Excel file linked to the Access table.

0
 
SiddharthRoutCommented:
Ah! I think the Access is making a change again in the closing event. Ok do this small exorcise.

Replace this line in the code

ret = MsgBox("Do you want to save this workbook", vbOKCancel)

with this

ret = MsgBox("Hello World", vbOKCancel)

Now close the workbook. Which message do you see first?

1) "Hello World" or

2) "Do you want to save the changes you made to ..."

Sid
0
 
currentdbAuthor Commented:
Replaced the line as you suggested. Now the message I see first is #2 "Do you want to save the changes you made to ..."

The #1 Hello World does not appear at all.

It's better to re-upload the same sheet, but now with the sheet that is linked to Access (even if you don't have Access).
0
 
SiddharthRoutCommented:
Yes that would help.

Sid
0
 
currentdbAuthor Commented:
Here's a sample.

There are 2 QUERY_FOR_QSL2 sheets.
The one that has a ''x'' mark at the end is old. The one without the ''x'' at the end works.

I tested both. The old one works, but of course, the 2 message box continue to appear. The other one (with blue lines) is from Access. As I worked to reduce it in size, data disappears once I'm trying to filter by acctopid 13. Nothing has changed as everything is still as it was before.
userform-sample-v7b-5.xlsm
0
 
SiddharthRoutCommented:
Ok Few things. This

Sub Sample()
Msgbox Sheets("QUERY_FOR_GSL2").AutoFilterMode
End Sub

gives me false even if the autofilter is ON. Seems like it doesn't recognize that as autofilter and that is the reason why you didn't get the "Hello World" message ;)

I could manually remove the autofilter by first clicking on Cell A1 and then removing the autofilter.

I am experimenting more. Will get back to you shortly.

Sid
0
 
currentdbAuthor Commented:
I managed to make it work, I mean data from the Access table that was not filtered. I was again the AY column, so I had to re-format it.

As for the filter, still trying to see what does not work.

I re-posted the sample.
userform-sample-v7b-5.xlsm
0
 
currentdbAuthor Commented:
No problem, take your time. We just posted at the same time. Hope you had time to read my post ID 35200442 :)
0
 
currentdbAuthor Commented:
I went in the Access table and the autofilter seems to be off, but looks on if I click on any column...weird.

I posted a sample Access db if you need to look furthermore.

Thanks.
master-file.mdb
0
 
SiddharthRoutCommented:
Ok. I have reset the connection to the database in the Excel file that I have. And I don't get the Message box that I have on the Workbook close event as it doesn't recognize the "Autofilter" as autofilter in Sheet "QUERY_FOR_GSL2". Investigating further I realized that the autofilter in a table behaves differently.

Try this code now :)

Option Explicit

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim Ret
    If Sheets("QUERY_FOR_GSL2").ListObjects("Tableau_master_file_1").ShowAutoFilter Then
        Sheets("QUERY_FOR_GSL2").ListObjects("Tableau_master_file_1").ShowAutoFilter = False
        Ret = MsgBox("Do you want to save this workbook", vbOKCancel)
        If Ret = vbOK Then ThisWorkbook.Save
    End If
End Sub

Open in new window


0
 
currentdbAuthor Commented:
Looks like I have another compile error (Error 9: this does not belong to the selection) and it highlights line 5.

I think it makes reference at Tableau_master_file_1..but where you do see it ? Would it not be ''master_file.mdb'' which it leads to the Access database ?
0
 
SiddharthRoutCommented:
Oh you uploaded a fresh file... in ID: 35200442. This file has a different table.

Try this code

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim Ret
    If Sheets("QUERY_FOR_GSL2").ListObjects("Tableau_master_file").ShowAutoFilter Then
        Sheets("QUERY_FOR_GSL2").ListObjects("Tableau_master_file").ShowAutoFilter = False
        Ret = MsgBox("Do you want to save this workbook", vbOKCancel)
        If Ret = vbOK Then ThisWorkbook.Save
    End If
End Sub

Open in new window


Sid
0
 
SiddharthRoutCommented:
>>>I think it makes reference at Tableau_master_file_1..but where you do see it ?

To see the table name paste this code in a module and then run it :)

Sub FindTablesName()
    Dim ws As Worksheet
    Dim objList As ListObject
    Set ws = Sheets("QUERY_FOR_GSL2")
    For Each objList In ws.ListObjects
        MsgBox objList.Name
    Next
End Sub

Open in new window


Sid
0
 
currentdbAuthor Commented:
Wow! That's very surprising!

And you are right, it works! The filter is removed for good :) Thanks!!! :)

BTW, the code to find the table name is very good. I'll remember it for future use.

Now I'll award points, you'll get most of them and rorya will get the other part as it was also part of his code. Let me know if it's ok for you.
0
 
SiddharthRoutCommented:
Definitely it is ok with me :)

Sid
0
 
currentdbAuthor Commented:
Thank you very much for your help on this one as it was not really an easy one. I hope it didn't make you drink a lot of coffee :)
Thanks again! :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 23
  • 23
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now