Syntax to select a row to delete based on a variable

stile

New Member
Joined
Aug 14, 2002
Messages
4
Sub BrickTracker()

I'm trying to go through a sheet and if the "B",x value is not equal to "Brick", then delete the whole row. I'm getting an erorr on the row selection part. My code is:

Sheets("Tracker").Select
FinalRow = Range("B500").End(xlUp).Row
' Loop through each row
For x = 6 To FinalRow
thisvalue = Range("B" & x).Value
If thisvalue <> "Brick" Then
{Rows("x:x").Select}
Selection.Delete Shift:=xlUp
End If
Next x

I'm getting a problem in the line that has { } around it ({} isn't in the code). How do I correctly tell it to select the current row based on x?
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

Asala42

Well-known Member
Joined
Feb 26, 2002
Messages
2,318
Sheets("Tracker").Select
FinalRow = Range("B500").End(xlUp).Row
' Loop through each row
For x = 6 To FinalRow
thisvalue = Range("B" & x).Value
If thisvalue <> "Brick" Then
{Rows("x:x").Select}
Selection.Delete Shift:=xlUp
End If
Next x

How about this line instead:

Cells(x, 1).EntireRow.Delete (up)
 

Forum statistics

Threads
1,144,510
Messages
5,724,786
Members
422,579
Latest member
Lekha mohanty

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
Top