VBA: Test if excel autofilter returned anything

excelpinto

Board Regular
Joined
Feb 14, 2008
Messages
53
Hey all,

I'm using the autofilter to dynamically sort some data. Here's what I'm looking to do: If I apply the filter, and I get at least one filtered row (doesn't matter how many or what it is), I apply some formatting. If criteria applied to the autofilter results in no rows returned, no formatting.

I just dont know how to check for the presence of any row being returned after the criteria is applied? Any ideas/examples? Thx.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
excelpinto,

Sample data before the macro:

Excel Workbook
AB
1Title ATitle B
211
322
433
544
655
711
822
933
1044
1155
1211
1322
1433
1544
1655
Sheet1



After the macro:

Excel Workbook
AB
1Title ATitle B
211
322
433
544
655
711
822
933
1044
1155
1211
1322
1433
1544
1655
Sheet1




Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Press and hold down the 'ALT' key, and press the 'F11' key.

On the 'Insert' menu, click 'Module'.

Copy the below code, and paste it into the Module.

Code:
Option Explicit
Sub Test()
    Dim FilteredRange As Range
    Dim rw As Range
    Application.ScreenUpdating = False
    With ActiveSheet.Range("A1")
        .AutoFilter Field:=1, Criteria1:="2"
        Set FilteredRange = ActiveSheet.AutoFilter.Range.SpecialCells(xlCellTypeVisible)
        For Each rw In FilteredRange.Rows
            If rw.Row > FilteredRange.Rows.Row Then
                'If visible cell, format row here
                With rw
                    .Font.FontStyle = "Bold"
                    .Interior.ColorIndex = 3
                End With
            End If
        Next
        .AutoFilter
    End With
    Application.ScreenUpdating = True
End Sub


Then run the "Test" macro.


Have a great day,
Stan
 
Upvote 0
More correctly relative to yours:
Rich (BB code):
<font face=Courier New>
Function FilterOk(Sh As Worksheet) As Boolean
  On Error Resume Next
  With Sh.AutoFilter.Range
    FilterOk = .SpecialCells(xlCellTypeVisible).Rows.Address <> .Rows(1).Address
  End With
End Function

Sub Test_FilterOk()
  Debug.Print FilterOk(ActiveSheet)
End Sub
</FONT>
 
Last edited:
Upvote 0

Forum statistics

Threads
1,222,610
Messages
6,167,048
Members
452,093
Latest member
JamesFromAustin

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