Change the background of a cell based on another cell's background

simona123

New Member
Joined
Aug 26, 2017
Messages
10
Can somebody help me to change the background color of a cell depending on the color of another cell. I tried this:
Code:
Range("A1").Interior.Color = Range("A1").Interior.Color

It works. But my question is whether it can be done automatically? What i mean , is that every time I change the color in B1 to change it in A1 also. I do not normally work with VBA, that's why I'm asking.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Copy and paste this macro into the worksheet code module. Do the following: right click the tab for your sheet and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. Fill B1 with a color and exit the cell.
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim Colr
    Colr = Range("B1").Interior.Color
    Range("A1").Interior.Color = Colr
End Sub
 
Upvote 0
So long as your colors are set by interior fill rather than conditional format, you can copy the colors to other cells. You need to be more specific about how you want the colors to automatically work. If you are only working with columns A and B then this would automatically copy the adjacent cell color if a cell in A or B columns is changed.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Target.Column = 1 Then
    Target.Interior.Color = Target.Offset(, 1).Interior.Color
ElseIf Target.Column = 2 Then
    Target.Interior.Color = Target.Offset(, -1).Interior.Color
End If
End Sub

This code should be copied to the Worksheet code module. It will run when changes are made to the sheet.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,623
Latest member
Techenthusiast

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