Copying Rows With Highlighted Cells Multiple Columns

barim

Board Regular
Joined
Apr 19, 2006
Messages
176
Hello,

I have this code that extracts rows with highlighted cells based on which column I point to. Right now it points to column "C".
My data range spans from Column C through Column N.
Each time I run this code I manually change column reference. Highlighted cells can be found anywhere between Columns C:N.
Is there a way to copy all of the rows with one click without the need to manually change column reference?

This is the piece of code that I change each time:
Code:
If Sheets("1").Range("C" & i).Interior.ColorIndex <> xlNone Then

This is the whole macro:

Code:
Sub CopyHighlightedRows()
   Dim LastRow As Long
   Dim i As Long, j As Long

   With Worksheets("1")
   LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
   End With

   With Worksheets("Report")
   j = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
   End With

   For i = 2 To LastRow
       With Worksheets("1")
           If Sheets("1").Range("C" & i).Interior.ColorIndex <> xlNone Then
               .Rows(i).Copy Destination:=Worksheets("Report").Range("A" & j)
               j = j + 1
           End If
       End With
   Next i
End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
How's this looking?

Code:
Sub CopyHighlightedRows()
   Dim LastRow As Long
   Dim i As Long, j As Long, C As Long


   With Worksheets("1")
   LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
   End With


   With Worksheets("Report")
   j = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
   End With
   
   C = 3
   
For C = 3 To 14
   For i = 2 To LastRow
       With Worksheets("1")
       Sheets("1").Cells(i, C).Select
           If Sheets("1").Cells(i, C).Interior.ColorIndex <> xlNone Then
               .Rows(i).Copy Destination:=Worksheets("Report").Range("A" & j)
               j = j + 1
           End If
       End With
   Next i
i = 2
Next C
   
End Sub
 
Upvote 0
Sir, thank you so much for your response. I tested it and I had to cancel, because it ran for too long. I have a lot of data. Is there a way to speed up this process?
Thanks again.
 
Upvote 0
Maybe a better way would be to do an IF OR when looking at the cells? To see if any are highlighted?

So, your original with:


Code:
If Sheets("1").Range("C" & i).Interior.ColorIndex <> xlNone or Sheets("1").Range("D" & i).Interior.ColorIndex <> xlNone....etc Then
 
Upvote 0

Forum statistics

Threads
1,214,427
Messages
6,119,419
Members
448,895
Latest member
omarahmed1

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