Delete Cells If Does Not Equal Criteria

Bench

Board Regular
Joined
Aug 27, 2009
Messages
134
Hi,

Am writing a Sub that will delete all rows that do not contain 1 in Column B.

So far i have

Code:
Sub Macro14()
    
Dim ws As Worksheet
Set ws = ActiveSheet
For i = ws.Range("B5").End(xlDown).End(xlUp).Row To 1 Step -1
    If ws.Cells(i, 2) <> "1" Then
    ws.Rows(i).Delete
        End If
    Next
End Sub

Unfortunatley this deletes the "Total Row" and the "Headings"

Ideally i'd like to set the range first as this changes each time but not sure how to do this.

The code for the range that the For Next should run on is.

Code:
Range("B5", Range("A5").End(xlDown).Offset(-1, 1)).Select

Where do i go from here.

Thanks as always for all your help.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
If your Total Row is the last row and your Heading is in Row 1, this should do it:
Code:
For i = ws.Range("B5").End(xlDown).End(xlUp).Row - 1 To 2 Step -1
 
Upvote 0
If your Total Row is the last row and your Heading is in Row 1, this should do it:
Code:
For i = ws.Range("B5").End(xlDown).End(xlUp).Row - 1 To 2 Step -1

Thanks

Tried it without any luck, My Header is A4:C4 and the Total Row will vary.

The column that is being checked is Column B if that helps?
 
Upvote 0

Forum statistics

Threads
1,215,501
Messages
6,125,169
Members
449,212
Latest member
kenmaldonado

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