How to Automatically Link a Cell Color to Another in Excel

To demonstrate how to link a cell color to another automatically, we’ll use the following dataset containing some subjects in column B and corresponding marks in column D.

Let’s automatically link the color in cell range B5:B8 to another cell range D5:D8 using Excel VBA Macros. The code will change the color automatically when the referencing cell changes.

Step-by-Step Guidelines to Automatically Link a Cell Color to Another in Excel


Step 1 – Open Visual Basic Editor

  • Go to the Developer tab on the ribbon.
  • Click on Visual Basic to open the Visual Basic Editor, or press Alt+F11.

Step-by-Step Guidelines to Automatically Link a Cell Color to Another in Excel

  • Alternatively, right-click on your worksheet and from the context menu select View Code.


Step 2 – Write VBA Code to Link a Cell Color to Another

  • Enter the following code in the module window:

VBA Code:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Me.Range("D5").Interior.Color = Me.Range("B5").Interior.Color
    Me.Range("D6").Interior.Color = Me.Range("B6").Interior.Color
    Me.Range("D7").Interior.Color = Me.Range("B7").Interior.Color
    Me.Range("D8").Interior.Color = Me.Range("B8").Interior.Color
End Sub
  • Press Ctrl+S to save the code using the file-type Macro-enabled.

Step-by-Step Guidelines to Automatically Link a Cell Color to Another in Excel

VBA Code Explanation

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

The Private Sub or subprocedure names our procedure Worksheet_SelectionChange.

ByVal Target As Range sets the Range in the variable Target.

Me.Range("D5").Interior.Color = Me.Range("B5").Interior.Color
Me.Range("D6").Interior.Color = Me.Range("B6").Interior.Color
Me.Range("D7").Interior.Color = Me.Range("B7").Interior.Color
Me.Range("D8").Interior.Color = Me.Range("B8").Interior.Color

These lines link each cell’s color to the color of a corresponding cell.

End Sub

This line closes the procedure.


Step 3 – Final Output

  • Go back to your worksheet.

If you change the color of the referencing cells B5, B6, B7 or B8, this will automatically change the color in cells D5, D6, D7, D8 respectively.

Step-by-Step Guidelines to Automatically Link a Cell Color to Another in Excel


Conditional Formatting to Color a Cell in Excel

We can alternatively use conditional formatting to color a cell in Excel.

The dataset below contains some subjects in column B and marks for each subject in column C. Let’s color the cells with marks greater than 90.

Steps:

  • Go to the Home tab on the ribbon.
  • Under the Styles group, open the Conditional Formatting drop-down menu.
  • Click on Greater Than from the Highlight Cells Rules drop-down menu.

  • In the Greater Than dialog box that opens, enter the number which will fulfill the condition, here 90.
  • Select the color Light Red Fill with Dark Red Text.
  • Click on OK.

All the numbers greater than 90 are now colored.


Download Practice Workbook


Related Articles

<< Go Back To Excel Link Cells | Linking in Excel | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Sabrina Ayon
Sabrina Ayon

Sabrina Ayon, a Computer Science and Engineering graduate from United International University, has been an integral part of the ExcelDemy project for two years. She authored 150+ articles, excelling in instructing through visually engaging Excel tutorials. With a passion for teaching, Sabrina conducted sessions on Excel VBA, sharing her knowledge and insights with others. Currently holding the position of Project Manager for the ExcelDemy Visual Development Project, she oversees various aspects of the project, ensuring its smooth operation... Read Full Bio

2 Comments
  1. How would you do this if the cells you are wanting to link are in different sheets?

    • Hello, KRISTIN!
      I’m sorry to say that, it won’t work for linking a cell color to a different sheets.
      Even we can not do that actually. But you can copy the color format from a sheet cell and paste that into the differnet sheet.

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo