VBA code to open, remove autofilter, save and close multiple files in same directory

GSH

New Member
Joined
Apr 19, 2012
Messages
8
Hi there,

I was after some help...

I have around 3500 workbooks in a different subfolders within one all encompassing folder.

These contain around 5 tabs each which have autofilters applied in varying ways. I.e. A user has preivously opened the file and filtered the list and then saved the file without removing the filter.

I was tasked with copying all of the data from these 3500 workbooks into one new workbook. All of the 3500 files have one tab in particular that I needed the data copying/copying from, and so to do this I used an add-in created by Ron de Bruin which merges specific data from multiple workbooks and pastes them into a new sheet, which is exactly what I need. However this add-in only copies the data that is actually displayed in each file, and not the data that has been filtered out.

Obviously I need to make sure all of the files are unfiltered before I run the copy/merge add-in, but I am struggling to find a way to do this without going into 3500 separated workbooks and manually remove the filters.

I have searched the forums and found that pre 2007 there was a function called Application.FileSearch which has bnow been replaced with the Dir() function, but I am struggling to modify this to my needs.


Any help would be greatly appreciated.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Hi GSH and welcome to the forum,

Insert new Module and copy the code to the Module's code window
Rich (BB code):
Option Explicit

Sub RemoveFilters()

    Const csFilesPattern As String = "*.xls*"
    Dim sFileName As String, _
        sFolderName As String
    Dim iCounter As Integer
    Dim Wbk As Workbook
    Dim Wsh As Worksheet

    '* To start with, use the folder of ThisWorkbook
    sFolderName = Left(ThisWorkbook.FullName, InStrRev(ThisWorkbook.FullName, "\"))
    '* Select folder
    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .Title = "Select a Folder"
        .InitialFileName = sFolderName
        .Show
        If .SelectedItems.Count = 0 Then
            Exit Sub
        Else
            sFolderName = .SelectedItems(1) + "\"
        End If
    End With
    '*
    '* Check if file already exists
    sFileName = Dir(sFolderName + csFilesPattern, vbNormal)
    If sFileName = "" Then
        MsgBox "No Excel files found in the folder " & vbNewLine & sFolderName & vbNewLine & _
            "Please check the folder and try again.", vbCritical, "Files Not Exist"
        Exit Sub
    End If
    '*
    '* Open files and remove filters
    Application.ScreenUpdating = False
    Do While sFileName <> ""
        iCounter = iCounter + 1
        sFileName = sFolderName + sFileName
        Set Wbk = Workbooks.Open(sFileName)
        For Each Wsh In Wbk.Worksheets
            With Wsh
                If .AutoFilterMode Then
                    Wsh.AutoFilterMode = False
                End If
                If Wsh.FilterMode Then
                    Wsh.ShowAllData
                End If
            End With
        Next Wsh
        '*
        '* Save and close the workbook
        Wbk.Save
        Wbk.Close
        sFileName = Dir()
    Loop
    Application.ScreenUpdating = True
    MsgBox "Total files processed: " & iCounter, vbOKOnly, "Statistics"

End Sub

:eek:Please test the code on sample files before applying it to all your files.
 
Last edited by a moderator:
Upvote 0
Hi Mohammed,

Many thanks indeed for your reply. I will test this on my data tomorrow and report back with my findings.

Really appreciate your help!

Kind regards
Greg
 
Upvote 0
Hi there, I wondered if there was a way to include xlsx and xlsm files in the search? Would I have to amed the 3rd line of code to include these filetypes?

Thanks in advance
 
Upvote 0
Mohammed, this code works like a charm. Thanks! There is one thing that could make it slightly better, and that is to have it recursively searching through subdirectories as well. Is this possible?
 
Upvote 0
Al-hamdullah, thank you for the feedback!

Yes, it is possible to search subfolders, but I cannot promise you now on a timely response.

Will let you know once I finish it, but, though you have motivated me:), do not expect it to be soon.
 
Upvote 0

Forum statistics

Threads
1,217,441
Messages
6,136,649
Members
450,022
Latest member
Joel1122331

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