EntireRow.Delete not deleting all rows

stevec114

New Member
Joined
Feb 18, 2011
Messages
3
Hi Everyone,

I have created a script that deletes a row based on a string found in the first column (referenced to another table with a list of strings to delete)

What is weird is that the script works but is not deleting all rows that match the criteria. For example, if there are 10 rows that match the criteria the macro will delete 6 of them and leave the other 4 rows.

I have tried changing the code and doing some debugging and I am stuck.

Code:
Sub Create_Focus()
 
Dim excel_app As Object
Dim excel_sheet As Object
Dim iMaterial As Variant
Dim LensLastRow As Variant
Dim FocusLastRow As Variant
Dim FocusSRow As Variant
Dim LensSRow As Variant
 
'Removes Lens Materials from Focus tab on Template File
Windows("ELOA_Templates.xls").Activate
Worksheets("Focus").Select
FocusLastRow = ActiveSheet.UsedRange.Rows.Count
FocusSRow = 2
Do
Windows("ELOA_Templates.xls").Activate
   iMaterial = Range("A" & FocusSRow).Value
 
'Remove Material from LENS
Windows("ELOA_MASTER.xlsm").Activate
Worksheets("LENS").Select
LensLastRow = ActiveSheet.UsedRange.Rows.Count
LensSRow = 2
Do
If Cells(LensSRow, 1) = iMaterial Then Cells(LensSRow, "A").EntireRow.Delete
'If Cells(LensSRow, 1) = iMaterial Then Range("K" & LensSRow) = 0
LensSRow = LensSRow + 1
Loop While LensSRow <= LensLastRow
FocusSRow = FocusSRow + 1
 
Loop While FocusSRow <= FocusLastRow

I originally wrote the script by using For statements ('For LensSRow =2 To LensLastRow'), either method does not make a difference. I have also tried to declare the variables and long, string, and variable to see if that made any difference.

To test the loops and the If statement I added one line of code to change the value of column k on the row to '0' when the same criteria is met. The line is commented out above, but all of rows that matched the criteria were updated correctly and the code runs fine.

I also noticed that if I add multiple lines of the same criteria to the "Focus" sheet then the amount of rows deleted increases when I run the script.

I have pretty much narrowed it down to the actual EntireRow.Delete statement.

I am officially lost, hopefully someone has a better idea of what is happening.

I would appreciate your help!
 
Welcome to the forums!

You actually need to loop backwards through the rows. Go back to the FOR loop method, but use:

Code:
For LensSRow = LensLastRow To 2 Step -1
 
Upvote 0
Thank you! That worked perfectly.

It's good to know you have to loop backwards when using EntireRow.Delete.
Are there any other statements where you have to loop backward?

Steve
 
Upvote 0
Any time you have to loop through a column and you add or delete rows (Shift:=xlDown), you need to loop backwards.
 
Upvote 0

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