vba to delete rows base on criteria

cjcass

Well-known Member
Joined
Oct 27, 2011
Messages
679
Office Version
  1. 2016
Platform
  1. Windows
Hi,
I have the code below which works but has an issue in that it always deletes Row 9 whether it meets the criteria or not - any help much appreciated.
Thanks
VBA Code:
Dim lr&
Application.ScreenUpdating = False
ActiveSheet.AutoFilterMode = False
lr = Cells(Rows.Count, "CZ").End(xlUp).Row
    With Range("CZ9:CZ" & lr)
        .AutoFilter field:=1, Criteria1:="<" & Range("CM3").Value
        .SpecialCells(xlCellTypeVisible).EntireRow.delete
    End With
ActiveSheet.AutoFilterMode = False
Application.ScreenUpdating = True
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Not tested.
VBA Code:
    Dim lr&
    Dim FilterRange As Range, DataRange As Range, DeleteRange As Range
    Application.ScreenUpdating = False
    ActiveSheet.AutoFilterMode = False
    lr = Cells(Rows.Count, "CZ").End(xlUp).Row
    Set FilterRange = Range("CZ8:CZ" & lr)
    Set DataRange = FilterRange.Offset(1).Resize(FilterRange.Rows.Count - 1)
    FilterRange.AutoFilter field:=1, Criteria1:="<" & Range("CM3").Value
    Set DeleteRange = Application.Intersect(FilterRange.SpecialCells(xlCellTypeVisible).EntireRow, DataRange.EntireRow)
    If Not DeleteRange Is Nothing Then
        DeleteRange.Delete                            'delete blank rows
    End If
    ActiveSheet.AutoFilterMode = False
    Application.ScreenUpdating = True
 
Upvote 0
Solution
@cjcass AutoFilter always requires a header row (i.e. one row above the data), which is why your first row (row 9) remains visible whatever the criteria and is picked up by the SpecialCells.

If your data starts in row 9 then you need to make your range start on row 8 and then offset it as @rlv01 has done in the code in post number 2.

You don't actually need the SpecialCells as only the visible cells are deleted when you use .Delete on filtered cells if none of the rows are manually hidden.
 
Last edited:
Upvote 0
Not tested.
VBA Code:
    Dim lr&
    Dim FilterRange As Range, DataRange As Range, DeleteRange As Range
    Application.ScreenUpdating = False
    ActiveSheet.AutoFilterMode = False
    lr = Cells(Rows.Count, "CZ").End(xlUp).Row
    Set FilterRange = Range("CZ8:CZ" & lr)
    Set DataRange = FilterRange.Offset(1).Resize(FilterRange.Rows.Count - 1)
    FilterRange.AutoFilter field:=1, Criteria1:="<" & Range("CM3").Value
    Set DeleteRange = Application.Intersect(FilterRange.SpecialCells(xlCellTypeVisible).EntireRow, DataRange.EntireRow)
    If Not DeleteRange Is Nothing Then
        DeleteRange.Delete                            'delete blank rows
    End If
    ActiveSheet.AutoFilterMode = False
    Application.ScreenUpdating = True
Thank you very much indeed that works perfectly! :)
 
Upvote 0
@cjcass AutoFilter always requires a header row (i.e. one row above the data), which is why your first row (row 9) remains visible whatever the criteria and is picked up by the SpecialCells.

If your data starts in row 9 then you need to make your range start on row 8 and then offset it as @rlv01 has done in the code in post number 2.

You don't actually need the SpecialCells as only the visible cells are deleted when you use .Delete on filtered cells if none of the rows are manually hidden.
Thank you for the explanation - it now makes sense to me why it wasn't working - I had a hunch it was something to do with the header but I couldn't work it out.
 
Upvote 0

Forum statistics

Threads
1,215,045
Messages
6,122,830
Members
449,096
Latest member
Erald

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