VBA to copy range if interior.color is grey till next grey row

Akbarov

Active Member
Joined
Jun 30, 2018
Messages
347
Office Version
  1. 365
Platform
  1. Windows
Hello community,

I am trying to write VBA code which will copy data from non-grey cell in column C until first grey cell ( RGB 191,191,191 )
Then loop from where it left , to copy until next grey cell.
Row 1,2,3 are headers. So first copy-paste should be from row 4 to 13
Then continue from 13 to 20 and so on..

I tried to do as in following VBA. But I do something wrong..
When I debug:
lastrow = 173 ( which is correct )
endrow = 13 ( which is also correct )
startrow = <--- this is always 0 , I think problem is here or may be I do bigger mistakes.

I will appreciate if anyone can help me to solve this..


VBA Code:
  Sub copyBYcolor
  Dim rownum As Long
   Dim colnum As Long
   Dim startrow As Long
   Dim endrow As Long
   Dim lastrow As Long
   rownum = 3
   colnum = 3
   lastrow = ActiveSheet.Range("C65536").End(xlUp).Row
   With ActiveSheet.Range("C3:C" & lastrow)


   For rownum = 3 To lastrow
    Do
       If .Cells(rownum, 3).Interior.Color = RGB(191, 191, 191) Then
          startrow = rownum
       End If

       rownum = rownum + 1


   If (rownum > lastrow) Then Exit For

   Loop Until .Cells(rownum, 3).Interior.Color = RGB(191, 191, 191)
   endrow = rownum
   rownum = rownum + 1

   ActiveSheet.Range(Cells(startrow, 2), Cells(endrow, 17)).Copy

   'Sheets("Result").Select
   'Range("A1").Select
   'Sheets("Result").PasteSpecial xlPasteValuesAndNumberFormats


   Next rownum
   End With
   End Sub

I also asked this question in different forum, so this is cross-post link: VBA to copy between 2 rows based on cell's interior color
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
I am trying to write VBA code which will copy data from non-grey cell in column C.........
Hello Akbarov,

Could you not just filter on the non coloured cells? A short code could do this for you.

Cheerio,
vcoolio.
 
Upvote 0
Thanks for reply, but I need to copy paste each range to new workbook, I think it is only way to achieve this.
 
Upvote 0

Forum statistics

Threads
1,214,976
Messages
6,122,543
Members
449,089
Latest member
davidcom

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