Hi All,
I'm having an issue with this code. Sometimes it runs and sometimes it decides to not respond. I'm currently running the code from a master workbook that each of these filtered reports will be carried over into. I have about 10 files that are sent as reports every week and would like to apply an autofilter to them. I will need to filter different rows and different sheets for each excel file, thus I cannot simply loop through all of them. When the code works I try adding another file to the folder and more code to autofilter that file, but that's when the macro no longer works. Am I trying to loop through too many if/elseif statements? Any help would be much appreciated.
Option Explicit
Sub test()
Dim MyPath As String
Dim MyFile As String
Dim Wkb As Workbook
Dim Cnt As Long
Dim v As Variant
Dim ws As Worksheet
Application.ScreenUpdating = False
MyPath = "C:\Users\Documents" 'change the path accordingly
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
MyFile = Dir(MyPath & "*.xlsx")
v = Application.Transpose(Worksheets("sheet1").Range("Filtered Selection")) 'Named range that contains the list I need to filter
Cnt = 0
Do While Len(MyFile) > 0
Cnt = Cnt + 1
If Left(MyFile, 8) = "Example1" Then
Set Wkb = Workbooks.Open(MyPath & MyFile)
Sheets("Data Sheet").Select
Range("a1").Parent.AutoFilterMode = False
Range("a1").AutoFilter Field:=6, Criteria1:=v, Operator:=xlFilterValues
Wkb.Close savechanges:=True
MyFile = Dir
ElseIf Left(MyFile, 8) = "Example2" Then
Set Wkb = Workbooks.Open(MyPath & MyFile)
Sheets("Data Sheet4").Select
Range("a1").Parent.AutoFilterMode = False
Range("a1").AutoFilter Field:=8, Criteria1:=v, Operator:=xlFilterValues
Wkb.Close savechanges:=True
MyFile = Dir
ElseIf Left(MyFile, 8) = "Example3" Then
Set Wkb = Workbooks.Open(MyPath & MyFile)
Sheets("Data Sheet Example").Select
Range("a1").Parent.AutoFilterMode = False
Range("a1").AutoFilter Field:=3, Criteria1:=v, Operator:=xlFilterValues
Wkb.Close savechanges:=True
MyFile = Dir
ElseIf Left(MyFile, 15) = "Utilized Folder" Then
Set Wkb = Workbooks.Open(MyPath & MyFile)
For Each ws In ActiveWorkbook.Worksheets
ws.Visible = xlSheetVisible
Next ws
Sheets("Data3").Select
Range("a1").Parent.AutoFilterMode = False
Range("a1").AutoFilter Field:=5, Criteria1:=v, Operator:=xlFilterValues
Wkb.Close savechanges:=True
MyFile = Dir
ElseIf Left(MyFile, 7) = "Surveys" Then
Set Wkb = Workbooks.Open(MyPath & MyFile)
Sheets("Compliant Data").Select
Range("a1").Parent.AutoFilterMode = False
Range("a1").AutoFilter Field:=4, Criteria1:=v, Operator:=xlFilterValues
Wkb.Close savechanges:=True
MyFile = Dir
ElseIf Left(MyFile, 9) = "*Quarterly" Then
Set Wkb = Workbooks.Open(MyPath & MyFile)
Sheets("Quarter Data").Select
Range("a1").Parent.AutoFilterMode = False
Range("a1").AutoFilter Field:=5, Criteria1:=v, Operator:=xlFilterValues
Wkb.Close savechanges:=True
MyFile = Dir
End If
Loop
If Cnt > 0 Then
MsgBox "Complete!", vbExclamation
Else
MsgBox "No files were found!", vbExclamation
End If
Application.ScreenUpdating = True
End Sub
Also, I would like to request some help with the use of wild cards. For example, I have a report that contains numbers that change with each report, but the name of the file also contains "Quarter". Would it be possible to just look for "Quarter" and open the file that matches?
Thank you. I appreciate the time and help!
Bookmarks