Search code examples
excelcellvba

Matching color of a cell with another cell using VBA


I am required to write some VBA code so that, if I assign it to a command button, by clicking it I should be able to match the color of cells C11 and cell C4.

Suppose I change the color of C11 to red, the color of C4 should change to red by click of a command button or automatically.

Can some one help me with this?


Solution

  • The following code copies the color value from cell "C11" to cell "C4":

    Range("C4").Interior.Color = Range("C11").Interior.Color
    

    As long as you work with valid Range-Objects, you can use different types to address the cells as fits your needs. For example the active selection (at least one cell or more):

    Selection.Interior.Color = Range("C11").Interior.Color