I hope this helps:
Option Explicit ' This means I must declare my variables. It's a good idea to start every macro this way.
Sub CopyData()
Dim cl As Range ' This is a "pointer" it points to one cell after another in a range
Dim sh As Worksheet ' I'm defining this as an alias for the worksheet name so I don't have to type a lot.
Dim MyRange As String ' This is the range of data to inspect
Dim RowNum As Long ' A row counter
Dim CurColor As String ' A string to hold the current color name so the program can "remember" it.
' Initalize variables.
MyRange = ("$C$5:$C$15")
RowNum = 5
Set sh = Sheets("Sheet1") ' Now I don't have to type Sheets("Sheet1") all the time.
CurColor = "" ' It probably defualts to this, but I like to set everythign explicitly
' I'm going to slide the "pointer" down the range one cell at a time.
For Each cl In sh.Range(MyRange)
' If I'm on the same color or if it's the first color
If cl.Value = CurColor Or RowNum = 5 Then
' Put the item (zero rows up or down and one row left of the pointer into the cell
sh.Cells(RowNum, 6) = cl.Offset(0, -1).Value ' We use .Value because cl is actually a cell location
' increcment the row counter
RowNum = RowNum + 1
Else ' There is a change in color
' Copy in the boilerplate text
sh.Range("$D$5:$D$7").Copy sh.Cells(RowNum, 6)
' increment the row counter by three since we copied in 3 rows of text
RowNum = RowNum + 3
' Put the item into the cell
sh.Cells(RowNum, 6) = cl.Offset(0, -1).Value
' Increment the row counter
RowNum = RowNum + 1
End If
' Remember the color - in the original code I had this in both parts of the if statement
CurColor = cl.Value
Next
End Sub
Bookmarks