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.
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
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
 
Upvote 0
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)
 
Upvote 0
What do you want to happen, when the value in col A is repeated?
 
Upvote 0
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.
 
Upvote 0
Do you want to copy sheet 6 for each duplicate in sheet2, or just copy sheet6 once with all the sheet2 values underneath?
 
Upvote 0
But do you want it to look like this


Excel 2013 32 bit
AB
1title1title 2
2sheet6row x
3sheet2row a
4sheet2row b
5sheet2row c
6
7sheet6row x
8sheet2row a
9sheet2row b
10sheet2row c
Sheet7


Or like this


Excel 2013 32 bit
AB
1title1title 2
2sheet6row x
3sheet2row a
4
5sheet6row x
6sheet2row b
7
8sheet6row x
9sheet2row c
10
11sheet6row x
12sheet2row a
Sheet7
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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