Hello everyone,
I have a macro that Advance filter data then exported to a new workbook and it works fine, however, I would like for this workbook to not be created if the output is empty (Criteria is not matched). This would avoid getting an empty workbook that only contains headers.
Is there any way to check whether the output of advanced filter will be empty?
Thank you.
My current code is
I have a macro that Advance filter data then exported to a new workbook and it works fine, however, I would like for this workbook to not be created if the output is empty (Criteria is not matched). This would avoid getting an empty workbook that only contains headers.
Is there any way to check whether the output of advanced filter will be empty?
Thank you.
My current code is
Code:
Sub FilterData()
Dim wbName As String
Dim ThisWB As Workbook
n = Workbooks.Count
wbName = Cells(1, 3).Value
Set ThisWB = ThisWorkbook
Set NewBook = Application.Workbooks.Add(1)
NewBook.SaveAs Filename:=ThisWB.Path & "\" & wbName
ThisWB.Sheets("Caisses").Range("A1").CurrentRegion.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:= _
ThisWB.Sheets("Filter").Range("A3:C4"), CopyToRange:=Workbooks(n + 1).Sheets(1).Range("A1"), Unique:=True
Columns.AutoFit
Workbooks(n + 1).Save
End Sub
Last edited: