How to look up Background Color and format new cell

Pestomania

Active Member
Joined
May 30, 2018
Messages
332
Office Version
  1. 365
Platform
  1. Windows
Good day,

Is there a way to have a macro complete the following task:

On sheet1, column A there is a number.
On sheet2, column A are numbers that some will duplicate sheet1.
On sheet2, Column B the cells have been formatted with a background color.
On sheet1, column B the cells need the matching formatting from sheet2 column B.

On sheet1, the macro would look at the value in column A, locate that value on sheet2 column A and copy the background from sheet2 column B to sheet1 column B.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
How about
Code:
Sub GetColour()
   Dim Cl As Range
   Dim Ws1 As Worksheet, Ws2 As Worksheet
   
   Set Ws1 = Sheets("Pcode")
   Set Ws2 = Sheets("sheet2")
   With CreateObject("scripting.dictionary")
      For Each Cl In Ws2.Range("A2", Ws2.Range("A" & Rows.count).End(xlUp))
         If Cl.Offset(, 1).Interior.ColorIndex = -4142 Then
            .Item(Cl.Value) = -4142
         Else
            .Item(Cl.Value) = Cl.Offset(, 1).Interior.Color
         End If
      Next Cl
      For Each Cl In Ws1.Range("A2", Ws1.Range("A" & Rows.count).End(xlUp))
         If .exists(Cl.Value) Then Cl.Offset(, 1).Interior.Color = .Item(Cl.Value)
      Next Cl
   End With
End Sub
 
Upvote 0
A bit Crude... but it seems to work...

Code:
Sub Foo()
Dim sLoc As String
Dim LR As Long
Dim Rng As Range, C As Range
LR = Range("A" & Rows.Count).End(xlUp).Row
Set Rng = Range("A2:A" & LR)  'Change CellReference of A3 to FIRST NUMERIC Cell Reference
Rng.Offset(, 1).Interior.Color = xlNone
For Each C In Rng
    With Worksheets("Sheet2").Columns("A")
        On Error Resume Next
        sLoc = .Find(What:=C).Address
        If Err = 0 Then
            If sLoc <> "" Then
            C.Offset(, 1).Interior.Color = .Range(sLoc).Offset(, 1).Interior.Color
            End If
        End If
        sLoc = ""
        On Error GoTo 0
    End With
Next C
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,171
Members
453,021
Latest member
Justyna P

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