vba help - autofilter and check Visible cells count

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

I want to autofilter a data and before deleting data
I want to check whether there are any visible cells after autofilter and then delete data.

I know two methods to validate.
1) 'If .Columns(1).SpecialCells(12).Count > 1 Then 'Criteria to validate
2) If WorksheetFunction.Subtotal(3, .Columns(8)) > 1 Then

Are there any other method to check visible cells?.. plz share


VBA Code:
Sub Autofilter_Testing()

Sub Check_Visible_Cells

Dim lastrow As Long
lastrow = Range("a" & Rows.Count).End(xlUp).Row


With Range("a1").CurrentRegion
    .AutoFilter field:=8, Criteria1:="<5"
    
    '-----Check Point 1 validate visible cells----------
    'If .Columns(1).SpecialCells(12).Count > 1 Then  'Criteria to validate
    
    '-----Check Point 2 validate visible cells -----
     If WorksheetFunction.Subtotal(3, .Columns(8)) > 1 Then
    
        .Offset(1).Resize(.Rows.Count - 1).EntireRow.Select
        .Cells.AutoFilter
    Else
        .Cells.AutoFilter
    End If

End With

End sub

--------------------Below is Sample Data----------

Book3
ABCDEFGHIJ
1No.AthleteNationSportYearsGamesGenderGoldSilverBronze
21Michael Phelps United StatesSwimming2000-2016SummerM2332
32Larisa Latynina Soviet UnionGymnastics1956–1964SummerF954
43Marit Bjørgen NorwayCross-country skiing2002–2018WinterF843
54Nikolai Andrianov Soviet UnionGymnastics1972–1980SummerM753
65Ole Einar Bjørndalen NorwayBiathlon1998–2014WinterM841
76Boris Shakhlin Soviet UnionGymnastics1956–1964SummerM742
87Edoardo Mangiarotti ItalyFencing1936–1960SummerM652
98Takashi Ono JapanGymnastics1952–1964SummerM544
109Paavo Nurmi FinlandAthletics1920–1928SummerM930
1110Birgit Fischer East GermanyCanoeing1980–2004SummerF840
12Bjørn Dæhlie NorwayCross-country skiing1992–1998WinterM840
1312Sawao Kato JapanGymnastics1968–1976SummerM831
14Jenny Thompson United StatesSwimming1992–2004SummerF831
1514Ryan Lochte United StatesSwimming2004–2016SummerM633
1615Dara Torres United StatesSwimming1984–2008SummerF444
1716Alexei Nemov RussiaGymnastics1996–2000SummerM426
1817Natalie Coughlin United StatesSwimming2004–2012SummerF345
Sheet2


Thanks
mg
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Is this to determine if you have something to copy after the filter?

Code:
Sub Check_Visible_Cells()
    Dim sh As Worksheet
    Dim rng As Range, frng As Range
    Dim lastrow As Long

    Set sh = ActiveSheet
    With sh
        lastrow = .Range("A" & Rows.Count).End(xlUp).Row
        Set rng = .Range("A1:J" & lastrow)

        With rng

            .AutoFilter Field:=8, Criteria1:=">24"
            On Error Resume Next
            Set frng = rng.Offset(1).Resize(lastrow - 1, 10).SpecialCells(xlCellTypeVisible)
            On Error GoTo 0

            If Not frng Is Nothing Then
                frng.Copy
            Else
                .Cells.AutoFilter
            End If

        End With

    End With

End Sub
 
Upvote 0
Hi Dave,

Thanks for sharing one more option, really helpful. (y)


Thanks
mg
 
Upvote 0

Forum statistics

Threads
1,214,927
Messages
6,122,309
Members
449,080
Latest member
jmsotelo

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