If filtered table shows no result macro

mark692

Active Member
Joined
Feb 27, 2015
Messages
321
Office Version
  1. 2016
Platform
  1. Windows
Is there a code for filtering a table? If the code filtered a table and shows no result the code will do nothing, but if the filtered table has a result it will select the result, is that possible? Thanks
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
1. What do you mean by "the code will do nothing"? Cancel the filter (i.e show all data)?
2. Is your table a real table (not just range)?

Maybe this:

Code:
[FONT=lucida console][COLOR=Royalblue]With[/COLOR] ActiveSheet.ListObjects([COLOR=brown]"Table1"[/COLOR])
        .Range.AutoFilter Field:=[COLOR=crimson]1[/COLOR], Criteria1:=[COLOR=brown]"h"[/COLOR]
     [COLOR=Royalblue]If[/COLOR] .Range.Columns([COLOR=crimson]1[/COLOR]).SpecialCells(xlCellTypeVisible).Cells.count = [COLOR=crimson]1[/COLOR] [COLOR=Royalblue]Then[/COLOR]
        .Range.AutoFilter
     [COLOR=Royalblue]Else[/COLOR]
        .DataBodyRange.Select
     [COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]If[/COLOR]

[COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]With[/COLOR][/FONT]
 
Upvote 0
Assuming that Sheet1 contains your table, and that your table is named Table1, try...

Code:
Option Explicit

Sub SelectFilteredRecords()


    Dim rngFilter As Range


    With Worksheets("Sheet1")
        If .FilterMode Then .ShowAllData
    End With


    With Worksheets("Sheet1").ListObjects("Table1").Range
        .AutoFilter field:=1, Criteria1:="x"
        On Error Resume Next
        Set rngFilter = .Offset(1, 0).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
        On Error GoTo 0
        If Not rngFilter Is Nothing Then
            rngFilter.Select
        End If
    End With
    
End Sub

Change the sheet name and table name, accordingly. Also, change the field and criteria for your filter, accordingly.

Hope this helps!
 
Upvote 0
Assuming that Sheet1 contains your table, and that your table is named Table1, try...

Code:
Option Explicit

Sub SelectFilteredRecords()


    Dim rngFilter As Range


    With Worksheets("Sheet1")
        If .FilterMode Then .ShowAllData
    End With


    With Worksheets("Sheet1").ListObjects("Table1").Range
        .AutoFilter field:=1, Criteria1:="x"
        On Error Resume Next
        Set rngFilter = .Offset(1, 0).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
        On Error GoTo 0
        If Not rngFilter Is Nothing Then
            rngFilter.Select
        End If
    End With
    
End Sub

Change the sheet name and table name, accordingly. Also, change the field and criteria for your filter, accordingly.

Hope this helps!


this work sir but can you add a code that will bring me to ''sheet 2'' if the filtered table has no result. thanks
 
Upvote 0
Try replacing...

Code:
        If Not rngFilter Is Nothing Then
            rngFilter.Select
        End If

with

Code:
        If Not rngFilter Is Nothing Then
            rngFilter.Select
        Else
            .AutoFilter 'clear filter (optional)
            Worksheets("Sheet2").Activate
        End If
 
Upvote 0
Try replacing...

Code:
        If Not rngFilter Is Nothing Then
            rngFilter.Select
        End If

with

Code:
        If Not rngFilter Is Nothing Then
            rngFilter.Select
        Else
            .AutoFilter 'clear filter (optional)
            Worksheets("Sheet2").Activate
        End If


thank you sir it work sorry for late reply :)
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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