Search color than search value than copy both rows

emukiss10

Board Regular
Joined
Nov 17, 2017
Messages
201
Hello Guys!

A little tricky VBA request.

first part of code looks like this: (if cells have interior color diff than white add "NO" to selected row to column L)
Code:
 Dim CColour As Long
 Dim e As Long


    CColour = RGB(255, 255, 255)
    
    For e = 2 To Cells(Rows.count, "A").End(xlUp).row
        If Cells(e, "E").Interior.Color <> CColour Then
[B]        Cells(e, "L") = "NO"[/B]
        End If
    Next e

I would like to delete that Bold statement and add complicated one.

if color <> white then

take number from cell A2 Sheets(2) (if in row 2 col E color do not match ofc),
search it in cell column A Sheets(6)
and if found copy entire row Sheets(6) to Sheets(7) (Sheets(7) has headers so start from row 2)
and the row A2 Sheets(2) to next row Sheets(7)
- add one blank row (with no data for estetics) and go on till end of data in column A Sheets(2)





--------------------------------
Mayby this code can be of some use (copyright FLUFF)

Code:
   Dim Cl As Range
   Dim rng As Range
   
   With CreateObject("scripting.dictionary")
      For Each Cl In Sheets(1).Range("F2", Sheets(1).Range("F" & Rows.count).End(xlUp))
         If Not .Exists(Cl.Value) Then .Add Cl.Value, Nothing
      Next Cl
      For Each Cl In Sheets(2).Range("E2", Sheets(2).Range("E" & Rows.count).End(xlUp))
         If Not .Exists(Cl.Value) Then
            If rng Is Nothing Then
               Set rng = Cl
            Else
               Set rng = Union(rng, Cl)
            End If
         End If
      Next Cl
   End With
   If Not rng Is Nothing Then rng.EntireRow.Delete


Best Regards
W.
 

Some videos you may like

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,898
Office Version
  1. 365
Platform
  1. Windows
How about
Code:
Sub CopyRowsBasedOnColour()

   Dim Cl As Range
   Dim Ws2 As Worksheet
   Dim Ws6 As Worksheet
   Dim Ws7 As Worksheet
   
   Set Ws2 = Sheets("Sheet2")
   Set Ws6 = Sheets("Sheet6")
   Set Ws7 = Sheets("Sheet7")
   
   With CreateObject("scripting.dictionary")
      For Each Cl In Ws2.Range("E2", Ws2.Range("E" & Rows.Count).End(xlUp))
         If Not Cl.Interior.Color = RGB(255, 255, 255) Then
            If Not .exists(Cl.Offset(, -4).Value) Then .Add Cl.Offset(, -4).Value, Cl.Offset(, -4)
         End If
      Next Cl
      For Each Cl In Ws6.Range("A2", Ws6.Range("A" & Rows.Count).End(xlUp))
         If .exists(Cl.Value) Then
            Cl.EntireRow.Copy Ws7.Range("A" & Rows.Count).End(xlUp).Offset(2)
            .Item(Cl.Value).EntireRow.Copy Ws7.Range("A" & Rows.Count).End(xlUp).Offset(1)
         End If
      Next Cl
   End With

End Sub
 

emukiss10

Board Regular
Joined
Nov 17, 2017
Messages
201
Hello Fluff,

It works but only for unique values in col A.

A E F and so on

1 white Different data in every column

2 white Different data in every column

3 white Different data in every column

4 white Different data in every column

5 white Different data in every column

6 white Different data in every column

7 Blue Different data in every column

7 Blue Different data in every column

8 white Different data in every column

9 white Different data in every column

10 white Different data in every column

It copy only first "7" to the final sheet. (the rest is working correctly)
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,898
Office Version
  1. 365
Platform
  1. Windows
What do you want to happen, when the value in col A is repeated?
 

emukiss10

Board Regular
Joined
Nov 17, 2017
Messages
201

ADVERTISEMENT

copy it like all the others. The value in col A may be the same but the rest of the values, rest of the columns are different.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,898
Office Version
  1. 365
Platform
  1. Windows
Do you want to copy sheet 6 for each duplicate in sheet2, or just copy sheet6 once with all the sheet2 values underneath?
 

emukiss10

Board Regular
Joined
Nov 17, 2017
Messages
201

ADVERTISEMENT

Copy duplicate. It is for visual verification and dthe data may be totally different. Please :)
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,898
Office Version
  1. 365
Platform
  1. Windows
But do you want it to look like this

<b>Excel 2013 32 bit</b><table cellpadding="2.5px" rules="all" style=";background-color: rgb(255, 255, 255);border: 1px solid;border-collapse: collapse; border-color: rgb(182, 170, 166)"><colgroup><col width="25px" style="background-color: rgb(240, 224, 224)" /><col /><col /></colgroup><thead><tr style=" background-color: rgb(240, 224, 224);text-align: center;color: rgb(32, 17, 22)"><th></th><th>A</th><th>B</th></tr></thead><tbody><tr ><td style="color: rgb(32, 17, 22);text-align: center;">1</td><td style=";">title1</td><td style=";">title 2</td></tr><tr ><td style="color: rgb(32, 17, 22);text-align: center;">2</td><td style=";">sheet6</td><td style=";">row x</td></tr><tr ><td style="color: rgb(32, 17, 22);text-align: center;">3</td><td style=";">sheet2</td><td style=";">row a</td></tr><tr ><td style="color: rgb(32, 17, 22);text-align: center;">4</td><td style=";">sheet2 </td><td style=";">row b</td></tr><tr ><td style="color: rgb(32, 17, 22);text-align: center;">5</td><td style=";">sheet2</td><td style=";">row c</td></tr><tr ><td style="color: rgb(32, 17, 22);text-align: center;">6</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: rgb(32, 17, 22);text-align: center;">7</td><td style=";">sheet6</td><td style=";">row x</td></tr><tr ><td style="color: rgb(32, 17, 22);text-align: center;">8</td><td style=";">sheet2</td><td style=";">row a</td></tr><tr ><td style="color: rgb(32, 17, 22);text-align: center;">9</td><td style=";">sheet2 </td><td style=";">row b</td></tr><tr ><td style="color: rgb(32, 17, 22);text-align: center;">10</td><td style=";">sheet2</td><td style=";">row c</td></tr></tbody></table><p style="width:3.6em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid rgb(182, 170, 166);border-top:none;text-align: center;background-color: rgb(240, 224, 224);color: rgb(32, 17, 22)">Sheet7</p><br /><br />

Or like this

<b>Excel 2013 32 bit</b><table cellpadding="2.5px" rules="all" style=";background-color: rgb(255, 255, 255);border: 1px solid;border-collapse: collapse; border-color: rgb(182, 170, 166)"><colgroup><col width="25px" style="background-color: rgb(240, 224, 224)" /><col /><col /></colgroup><thead><tr style=" background-color: rgb(240, 224, 224);text-align: center;color: rgb(32, 17, 22)"><th></th><th>A</th><th>B</th></tr></thead><tbody><tr ><td style="color: rgb(32, 17, 22);text-align: center;">1</td><td style=";">title1</td><td style=";">title 2</td></tr><tr ><td style="color: rgb(32, 17, 22);text-align: center;">2</td><td style=";">sheet6</td><td style=";">row x</td></tr><tr ><td style="color: rgb(32, 17, 22);text-align: center;">3</td><td style=";">sheet2</td><td style=";">row a</td></tr><tr ><td style="color: rgb(32, 17, 22);text-align: center;">4</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: rgb(32, 17, 22);text-align: center;">5</td><td style=";">sheet6</td><td style=";">row x</td></tr><tr ><td style="color: rgb(32, 17, 22);text-align: center;">6</td><td style=";">sheet2 </td><td style=";">row b</td></tr><tr ><td style="color: rgb(32, 17, 22);text-align: center;">7</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: rgb(32, 17, 22);text-align: center;">8</td><td style=";">sheet6</td><td style=";">row x</td></tr><tr ><td style="color: rgb(32, 17, 22);text-align: center;">9</td><td style=";">sheet2</td><td style=";">row c</td></tr><tr ><td style="color: rgb(32, 17, 22);text-align: center;">10</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: rgb(32, 17, 22);text-align: center;">11</td><td style=";">sheet6</td><td style=";">row x</td></tr><tr ><td style="color: rgb(32, 17, 22);text-align: center;">12</td><td style=";">sheet2</td><td style=";">row a</td></tr></tbody></table><p style="width:3.6em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid rgb(182, 170, 166);border-top:none;text-align: center;background-color: rgb(240, 224, 224);color: rgb(32, 17, 22)">Sheet7</p><br /><br />
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,898
Office Version
  1. 365
Platform
  1. Windows
How about
Code:
Sub CopyRowsBasedOnColour()

   Dim Cl As Range
   Dim Cnt As Long
   Dim Adrs As Variant
   Dim Ws2 As Worksheet
   Dim Ws6 As Worksheet
   Dim Ws7 As Worksheet
   
   Set Ws2 = Sheets("Sheet2")
   Set Ws6 = Sheets("Sheet6")
   Set Ws7 = Sheets("Sheet7")
   
   With CreateObject("scripting.dictionary")
      For Each Cl In Ws2.Range("E2", Ws2.Range("E" & Rows.Count).End(xlUp))
         If Not Cl.Interior.Color = RGB(255, 255, 255) Then
            If Not .exists(Cl.Offset(, -4).Value) Then
               .Add Cl.Offset(, -4).Value, Cl.Offset(, -4).Address
            Else
               .Item(Cl.Offset(, -4).Value) = .Item(Cl.Offset(, -4).Value) & "," & Cl.Offset(, -4).Address
            End If
         End If
      Next Cl
      For Each Cl In Ws6.Range("A2", Ws6.Range("A" & Rows.Count).End(xlUp))
         If .exists(Cl.Value) Then
            Adrs = Split(.Item(Cl.Value), ",")
            For Cnt = LBound(Adrs) To UBound(Adrs)
               Cl.EntireRow.Copy Ws7.Range("A" & Rows.Count).End(xlUp).Offset(2)
               Ws2.Range(Adrs(Cnt)).EntireRow.Copy Ws7.Range("A" & Rows.Count).End(xlUp).Offset(1)
            Next Cnt
         End If
      Next Cl
   End With

End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,122,459
Messages
5,596,266
Members
414,049
Latest member
MisterExcel26

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
Top