How to end macro if no filtered results

willow1985

Well-known Member
Joined
Jul 24, 2019
Messages
929
Office Version
  1. 365
Platform
  1. Windows
I have been trying a bunch of different ways but so far none are successful.

I am looking for a way that if there are no filtered results where I have put my note then the filter will be cleared on table named: WPG_RA, it will go to sheet called "Weekly", clear the contents of the table named "Weekly_R_A" and display msgbox: "No Results for the specified Criteria"

But I only want the code to do this if there are no filtered results, otherwise proceed as normal.

Any help would be greatly appreciated!

VBA Code:
Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub Week_Data()
'
' Week_Data Macro

Application.ScreenUpdating = False
Sheets("Weekly").Select
    Rows("11:11").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Delete Shift:=xlUp
Sheets("DATA").Select
Sheets("DATA").Range("A1:Z1000000").ListObject.QueryTable.Refresh BackgroundQuery:=False
Sleep 2
DoEvents
For Each lo In ActiveSheet.ListObjects
    lo.AutoFilter.ShowAllData
      Next lo
      If (ActiveSheet.AutoFilterMode And ActiveSheet.FilterMode) Or ActiveSheet.FilterMode Then
  ActiveSheet.ShowAllData
End If
    ActiveSheet.ListObjects("WPG_RA").Range.AutoFilter Field:=24, Criteria1:= _
       Worksheets("Weekly").Range("R2").Value
    ActiveSheet.ListObjects("WPG_RA").Range.AutoFilter Field:=18, Criteria1:= _
        Worksheets("Weekly").Range("Q2").Value
    ActiveSheet.ListObjects("WPG_RA").Range.AutoFilter Field:=25, Criteria1:= _
        Worksheets("Weekly").Range("P2").Value
        
'IF NO FILTERED RESULTS, CLEAR FILTER, GO TO SHEET "Weekly", Range("Weekly_R_A").Select Selection.ClearContents and display msgbox "No Results for the specified Criteria"
        
Range("A3").Select
    LastRowColumnA = Cells(Rows.Count, 1).End(xlUp).Row
    Range("A3:M" & LastRowColumnA).Select
    

    
    Selection.SpecialCells(xlCellTypeVisible).Copy
    Sheets("Weekly").Select
    Range("B10").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False

    Range("A10").AutoFill Destination:=Range("A10:A" & Cells(Rows.Count, "B").End(xlUp).Row), Type:=xlFillSeries
Calculate
    Range("A1").Select

    

Sheets("DATA").Select

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

Sheets("Weekly").Select

Application.ScreenUpdating = True

MsgBox "Weekly Report Complete"

End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Try:
VBA Code:
'IF NO FILTERED RESULTS
If ActiveSheet.ListObjects("WPG_RA").Range.Columns(1).SpecialCells(xlCellTypeVisible).Count = 1 Then
MsgBox "empty"
End If
 
Upvote 0
Solution
Try:
VBA Code:
'IF NO FILTERED RESULTS
If ActiveSheet.ListObjects("WPG_RA").Range.Columns(1).SpecialCells(xlCellTypeVisible).Count = 1 Then
MsgBox "empty"
End If
I modified it a bit but otherwise perfect. Thank you!

VBA Code:
If ActiveSheet.ListObjects("WPG_RA").Range.Columns(1).SpecialCells(xlCellTypeVisible).Count = 1 Then
ActiveSheet.ShowAllData
Sheets("Weekly").Select
    Range("Weekly_R_A").Select
    Selection.ClearContents
    Range("A1").Select
            MsgBox "No Results for the specified Criteria"
            Exit Sub
End If
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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