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
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
You need to repeat the last row line of code when the If statement has run
Rich (BB code):
Sub TestSub()
Dim iCell As Range, ws As Worksheet
Set ws = Sheet6
Lastrow = ws.UsedRange.Rows.Count
    For Each iCell In Range("A2:BD3")
        If iCell.Interior.ColorIndex = 35 Then
            ws.Cells(Lastrow, 1).Value = iCell.Text
            ws.Cells(Lastrow, 1).Offset(0, 1).Value = iCell.Offset(0, 1).Text
        End If
    Lastrow = ws.UsedRange.Rows.Count
    Next iCell
End Sub
 
Upvote 0
Thanks Michael, that's quite a bit cleaner than what I had going on. Still seems to be overwriting everything in the first 2 cells of sheet 6 though. It's going to pick up probably anywhere from 1 to 30 different cells with that color index within the if statement and it sticks them all in the first row on sheet 6
 
Upvote 0
Maybe your "lastRow" change is not impacting on Column "A"
Try this method... UNTESTED
Code:
Sub TestSub()
Dim iCell As Range, ws As Worksheet
Set ws = Sheet6
Lastrow = ws.Cells(Rows.Count, "A").End(xlUp).Row
    For Each iCell In Range("A2:BD3")
        If iCell.Interior.ColorIndex = 35 Then
            ws.Cells(Lastrow, 1).Value = iCell.value
            ws.Cells(Lastrow, 1).Offset(0, 1).Value = iCell.Offset(0, 1).value
        End If
        Lastrow = ws.Cells(Rows.Count, "A").End(xlUp).Row
    Next iCell
End Sub
 
Upvote 0
Rich (BB code):
Sub TestSub()
Dim iCell As Range, ws As Worksheet
Set ws = Sheet6
Lastrow = ws.Cells(Rows.Count, "A").End(xlUp).Row
    For Each iCell In Range("A2:BD3")
        If iCell.Interior.ColorIndex = 35 Then
            ws.Cells(Lastrow + 1, 1).Value = iCell.value
            ws.Cells(Lastrow + 1, 1).Offset(0, 1).Value = iCell.Offset(0, 1).value
        End If
        Lastrow = ws.Cells(Rows.Count, "A").End(xlUp).Row
    Next iCell
End Sub
 
Upvote 0
Whew....glad we got THAT sorted...(y)
 
Upvote 0
Since you are adding one row each time the color is found, consider this minor mod.
In addition to being more efficient, it will handle the possibility of blanks in Col A.

Code:
Sub TestSub()
Dim iCell As Range, ws As Worksheet
Set ws = Sheet6
Lastrow = ws.Cells(Rows.Count, "A").End(xlUp).Row
    For Each iCell In Range("A2:BD3")
        If iCell.Interior.ColorIndex = 35 Then
            ws.Cells(Lastrow + 1, 1).Value = iCell.value
            ws.Cells(Lastrow + 1, 1).Offset(0, 1).Value = iCell.Offset(0, 1).value
            [COLOR="#0000CD"]Lastrow=Lastrow+1
[/COLOR]        End If
    Next iCell
End Sub
 
Upvote 0
Hey Jerry....fair call there...One of my many failings, is assuming consistency...(y)
 
Upvote 0

Forum statistics

Threads
1,217,346
Messages
6,136,037
Members
449,979
Latest member
trinitybg10

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