VBA Help - If Filtered Range Return Nothing Then

Johnny Thunder

Well-known Member
Joined
Apr 9, 2010
Messages
693
Office Version
  1. 2016
Platform
  1. MacOS
Hello all,

I am having some issues with a statement in my current code and all sources online aren't really answering my question directly.

I have a fairly long script that loops thru a range to get a filter criteria value and then apply the filter to a range on a seperate sheet. This code works great but recently I added in some new values to the list of criteria that don't have any data on my filter range but will in the future.

I need a "IF Statement to determine if the Filter Range results in a nothing being returned and if so, GoTo Jump which will jump the code down past several other actions in the code and go to the next loop value.

Any ideas will be super helpful.

My filter range: ws.Range("B3:B" & LastR).SpecialCells(xlCellTypeVisible)
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Hi there,

My range has the text "Green" so when I filtered on "Red" there are no matches:

VBA Code:
Option Explicit
Sub Macro1()

    Dim ws As Worksheet
    Dim LastR As Long
    
    Set ws = Sheets("Sheet1")
    LastR = 20

    With ws.Range("B3:B" & LastR)
        .AutoFilter Field:=1, Criteria1:="Red"
        If Application.WorksheetFunction.Subtotal(3, .SpecialCells(xlCellTypeVisible)) = 0 Then
            GoTo Jump
        End If
    End With
    
    MsgBox "Code from here if there are records in the filtered range."
    
Exit Sub
    
Jump:

    MsgBox "There were no filtered records"

End Sub

Regards,

Robert
 
Upvote 0
My filter range: ws.Range("B3:B" & LastR).SpecialCells(xlCellTypeVisible)
If row 3 is the header row then try something like this. Any code between the asterisk lines will only be executed if there are visible results in the filtered range. If not, then the code will jump to the 'Code continues from here' line.

If your header row is actually row 2 then simply change the B3 in the code to B2.

VBA Code:
If ws.Range("B3:B" & LastR).SpecialCells(xlCellTypeVisible).Count > 1 Then
  '*************
  'Your other actions go here
  '*************
End If

'Code continues from here
 
Upvote 0
Thanks Trebor and Peter! I ended up going with Peters solution since it was pretty straight forward and a quick modification. Worked like a charm. Cheers
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,707
Members
448,981
Latest member
recon11bucks

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