Delete row based on cell Values

Javi09

New Member
Joined
Nov 2, 2022
Messages
16
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
Platform
  1. Windows
I have a worksheet that has over 1500 rows and I want to delete the row if it contains "I24" it does delete them but I have to run the macro multiple times. What can I do to my code to so I can just run it once?

For Each cell In Range("F1:F2000")
If cell.Value = "I24" Then
cell.EntireRow.Delete

End If
Next cell
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Since deleting rows shifts rows below up, if there are multiple rows in a row to delete, some will get missed.
Therefore, it is better to loop through the rows backwards when adding or deleting rows.

So try this:
VBA Code:
For r = 2000 to 1 Step -1
    If Cells(r, "F").Value = "I24" Then Rows(r).Delete
Next r
 
Upvote 0
Solution
Since deleting rows shifts rows below up, if there are multiple rows in a row to delete, some will get missed.
Therefore, it is better to loop through the rows backwards when adding or deleting rows.

So try this:
VBA Code:
For r = 2000 to 1 Step -1
    If Cells(r, "F").Value = "I24" Then Rows(r).Delete
Next r
What would I need to dim r as?
I get compile error: Variable not defined
 
Upvote 0
FIxed it, thanks!
You are welcome!

I am guessing that you have "Option Explicit" turned on, requiring you to declare all your variables (which is a very good idea!).
Since it is just a number, you can just do:
VBA Code:
Dim r as Long
 
Upvote 0

Forum statistics

Threads
1,213,560
Messages
6,114,304
Members
448,564
Latest member
ED38

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