Excel 2013 VBA loop, how to make skip to next row

jedwardo

Board Regular
Joined
Aug 21, 2012
Messages
122
Hi Guys,

New at loops here, this is copying the data I need to a new sheet but it's all landing in the same two cells and overwriting the previous data instead of skipping to the next row down each time before pasting the next.

Code:
Sub TestSub()Dim iCell As Range
Lastrow = Sheet6.UsedRange.Rows.Count
For Each iCell In Range("A2:BD3")


If iCell.Interior.ColorIndex = 35 Then
Sheet6.Cells(Lastrow, 1).Value = iCell.Text
Sheet6.Cells(Lastrow, 1).Offset(0, 1).Value = iCell.Offset(0, 1).Text
End If


Next iCell


End Sub
 
Hi Michael,

Even if we know there will be no blanks in Col A, what caught my eye was that on each loop, the code was repeating the process of searching for the last row.

That's a relatively fast process, but it's just not necessary - especially if the color criteria wasn't met and lastrow will be unchanged.
 
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