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

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
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,003
Messages
6,122,655
Members
449,091
Latest member
peppernaut

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