I'm trying to copy information from one worksheet to another. When the code below finds red colored text in column A in the srcWs (source worksheet) it copies the red color text and the preceding 6 rows into the dstWS (Destination worksheet).
I need to modify this code to only copy the the first instance of red colored text in Column A srcWs (source worksheet) plus the preceding 6 rows containing only black text and skip over (not copy) any additional red colored text. This information would be copied to the dstWS (Destination worksheet) and the code would cycle through the entire Source worksheet until completed.
Referring to my sample below you will see that row 6 contains red colored text. I want my code below to skip over this red colored text and copy row 8 backward 6 rows but skip row 6 because it contains red colored text. I then want the code to repeat this process for every instance of red colored text in column A.
Time MPIDInside Bid Inside Bid Tick
Row 1 7:43:13 C SCHB 50 1 77 1 (black colored text)
Row 2 7:43:13 C SCHB 50 1 77 1 (black colored text)
Row 3 7:46:10 C PRUS 51 1 57.5 1 (black colored text)
Row 4 7:46:11 C PRUS 51 1 58 1 (black colored text)
Row 5 7:46:14 C PRUS 51 1 58.5 1 (black colored text)
Row 6 7:46:14 C PRUS 51 1 59 1 (red colored text)
Row 7 7:46:16 C PRUS 51 1 59.5 1 (black colored text)
Row 8 7:46:16 C PRUS 51 1 60 1 (red colored text)
Sub CopyRedCells()
Dim srcWs As Worksheet, dstWs As Worksheet
Dim Cell As Range, Rng As Range, dstRng As Range
Dim lastRow As Long
Set srcWs = Sheets("MM_OSTK_01-12-2005AM")
Set dstWs = Sheets("Sheet4") 'change to your destination worksheet
lastRow = srcWs.Range("A" & Cells.Rows.Count).End(xlUp).Row
Set Rng = srcWs.Range("A2:A" & lastRow)
For Each Cell In Rng
With Cell.Font
If (.Color = vbRed And .Name = "Arial") And (Cell.Value <> "Time") Then
If dstWs.Range("A" & Cells.Rows.Count).End(xlUp).Row < 5 Then
Set dstRng = dstWs.Range("A5")
Else
Set dstRng = dstWs.Range("A" & Cells.Rows.Count).End(xlUp).Offset(2)
End If
srcWs.Rows(Cell.Row & ":" & Cell.Row - 6).Copy dstRng
End If
End With
Next Cell
End Sub
Thanks you input is greatly appreciated.
David
I need to modify this code to only copy the the first instance of red colored text in Column A srcWs (source worksheet) plus the preceding 6 rows containing only black text and skip over (not copy) any additional red colored text. This information would be copied to the dstWS (Destination worksheet) and the code would cycle through the entire Source worksheet until completed.
Referring to my sample below you will see that row 6 contains red colored text. I want my code below to skip over this red colored text and copy row 8 backward 6 rows but skip row 6 because it contains red colored text. I then want the code to repeat this process for every instance of red colored text in column A.
Time MPIDInside Bid Inside Bid Tick
Row 1 7:43:13 C SCHB 50 1 77 1 (black colored text)
Row 2 7:43:13 C SCHB 50 1 77 1 (black colored text)
Row 3 7:46:10 C PRUS 51 1 57.5 1 (black colored text)
Row 4 7:46:11 C PRUS 51 1 58 1 (black colored text)
Row 5 7:46:14 C PRUS 51 1 58.5 1 (black colored text)
Row 6 7:46:14 C PRUS 51 1 59 1 (red colored text)
Row 7 7:46:16 C PRUS 51 1 59.5 1 (black colored text)
Row 8 7:46:16 C PRUS 51 1 60 1 (red colored text)
Sub CopyRedCells()
Dim srcWs As Worksheet, dstWs As Worksheet
Dim Cell As Range, Rng As Range, dstRng As Range
Dim lastRow As Long
Set srcWs = Sheets("MM_OSTK_01-12-2005AM")
Set dstWs = Sheets("Sheet4") 'change to your destination worksheet
lastRow = srcWs.Range("A" & Cells.Rows.Count).End(xlUp).Row
Set Rng = srcWs.Range("A2:A" & lastRow)
For Each Cell In Rng
With Cell.Font
If (.Color = vbRed And .Name = "Arial") And (Cell.Value <> "Time") Then
If dstWs.Range("A" & Cells.Rows.Count).End(xlUp).Row < 5 Then
Set dstRng = dstWs.Range("A5")
Else
Set dstRng = dstWs.Range("A" & Cells.Rows.Count).End(xlUp).Offset(2)
End If
srcWs.Rows(Cell.Row & ":" & Cell.Row - 6).Copy dstRng
End If
End With
Next Cell
End Sub
Thanks you input is greatly appreciated.
David