For Each statement stopping after first loop

Papercut

New Member
Joined
May 24, 2019
Messages
6
Hello,

As the title suggests, I have a For Next loop that stops after the first loop. i am trying to copy the row of any cell that has that color in the cell. I have a work around with a do until but it takes almost 6 minutes to run. Will the For Next run faster? Also why does it stop after the first row?

Code:
lrow7 = Sheets("Combined").Cells(Rows.Count, "A").End(xlUp).Row
For Each a In Sheets("Combined").Range("C2:C" & Range("C40000").End(xlUp).Row)


    If a.Interior.ColorIndex = 40 Then
     lr = Sheets("Dupes").Cells(Rows.Count, "A").End(xlUp).Row + 1
     a.EntireRow.Copy Destination:=Sheets("Dupes").Range("A" & lr)
     a.EntireRow.Delete
    End If
Next a

Thank you
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Which sheet are you on when you call this code to run?

Note that you may have to change your "For" line to read:
Code:
For Each a In Sheets("Combined").Range("C2:C" & [COLOR=#ff0000]Sheets("Combined").[/COLOR]Range("C40000").End(xlUp).Row)
Otherwise, it will calculate the last row from whatever the ActiveSheet is at the time you are running it.
 
Upvote 0
You are welcome.
I thought the original call would be enough.
No, everything on the right-side of the "&" is independent of what it on the left-side of it (it is its own separate entity).
So you need to explicitly define exactly where that range is coming from, or it defaults to the ActiveSheet.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

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