Marco not counting rows correctly

BBATESJR15

New Member
Joined
Feb 23, 2018
Messages
3
Dim c As Range
Dim Remove As Integer

Remove = Sheets("TEST").Range("A" & Rows.Count).End(xlUp).Row


For Each c In Sheets("TEST").Range("A1:A" & Remove)


If Len(c) <= 8 Then
c.EntireRow.Delete
End If
Next c

Hi,

I'm using the code above to delete rows that have 8 characters or less. The code is working, but I have to run the macro a least 5-6 time before it removes all of the rows that have 8 characters or less. I have used this line of code a few times and I've never had this problem. It seems to a counting issue because it's skipping rows as it looks for the info. Any help would be greatly appreciated.

Thanks,

B
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
When deleting/inserting rows you should work from the bottom up.
The problem is that is you delete row 2, row 3 becomes row 2 & then you go to next c, which will be row 3 (the original row 4). So the original row 3 never gets checked. try
Code:
Dim c As Range
Dim Remove As Integer

Remove = Sheets("TEST").Range("A" & Rows.Count).End(xlUp).Row


For c = Remove To 1 Step -1
   If Len(Range("A" & c)) <= 8 Then
      Rows(c).EntireRow.Delete
   End If
Next c
 
Upvote 0
Apologies, missed that. It should be
Code:
Dim c As Long
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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