+ Reply to Thread
Results 1 to 7 of 7

Set a Cell Background Color to Another Cell’s Color

  1. #1
    Forum Contributor
    Join Date
    08-20-2013
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Excel 2010, 2016, 2019
    Posts
    110

    Set a Cell Background Color to Another Cell’s Color

    Hi All,

    My default background colour for the workbook is R0 | G36 | B52.

    I would like to change the colour of the entire workbook and any additional tabs that might be added by only changing the colour of C6, which is for all headers.

    I am currently using "range("Notes!A1").Interior.Color = range("C6").Interior.Color", which is working fine, but the issue comes in if a row is added, it is still updating "Notes!A1" and not "Notes!A2" for example.

    So I am thinking that it shoul "Look up for the default color (R0 | G36 | B52), throughout and change to the selected color in C6?

    I have attached the sample workbook.

    Thank you in advance.
    Attached Files Attached Files
    Last edited by Jacolene; 03-10-2020 at 08:20 AM.

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Set a Cell Background Color to Another Cell’s Color

    Hi Jacolene,

    a. As you have noticed, when you change a row and/or column VBA code does not adjust.
    b. You need a way to uniquely identify the cells you want to treat specially. Methods to do this include:
    (1) Background Color
    (2) Text Value
    (3) Location of the cell in relation to some known cel
    (4) Font Characteristic (e.g. Name, Size, Bold, Italics, …)
    c. In this case it seems like 'Font Color' is a good choice.
    By changing one RGB index a small amount, we can generate a different color number that the human eye can't differentiate.
    d. There is nothing wrong with merged cells, but they are occasionally difficult to work with using VBA.
    In cases like yours, when it looks like you are only using one row and want to center the text, usng 'Center Across Selection' Formatting may be better suited (for future reference)
    e. It looks like all your 'special cells' are constants (not formulas) so that makes them easier to work with.
    f. I created 3 Main Macros to do what your want:
    (1) aaaChangeFontColorOfSelectedCellsToRGB255_254_255() changes the Font Color of the Selected Cell or Cells (can be run using Alt F8)
    (2) IdentifyAllOffWhiteFontsInWorkbook() Identifies all Cells that will Change Background Color (see Yellow CommandButton Shape on Sheet 'Edit')
    (3) ChangeBackgroundColorforAllCellsWithOffWhiteFontInWorkbook() changes the Background Color of Cells with 'Off White' Font Color (see Yellow CommandButton Shape on Sheet 'Edit')

    Current Font Color: vbWhite = 16777215 = RGB(255, 255, 255)
    New Font Color for Selected Cells: 16776959 = RGB(255, 254, 255)


    See the attached modified copy of your sample file which contains the following code in Ordinary Code Module ModChangeCellBackgroundColor:
    Please Login or Register  to view this content.
    To prevent typos from ruining days and weeks of work 'Option Explicit' is NEEDED at the top of each code module. This prevents errors caused by missspellings and FORCES every variable to be DECLARED (e.g. Dim i as Integer). http://www.cpearson.com/excel/DeclaringVariables.aspx

    Lewis

    Attachment deleted due to runtime error. See file associated with post #4 in this thread for corrected attachment. LJM
    Last edited by LJMetzger; 03-27-2020 at 10:50 AM. Reason: Deleted attachment due to runtime error

  3. #3
    Forum Contributor
    Join Date
    08-20-2013
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Excel 2010, 2016, 2019
    Posts
    110

    Re: Set a Cell Background Color to Another Cell’s Color

    Hi Lewis, thank you so much for the in depth explanation.

    When I ran the below 2 Macros, I received an error message: Invalid use of Null - Ln83 Col9 (iColorRGB = r.Font.Color)
    • ChangeBackgroundColorforAllCellsWithOffWhiteFontInWorkbook
    • IdentifyAllOffWhiteFontsInWorkbook

  4. #4
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Set a Cell Background Color to Another Cell’s Color

    Hi,

    I apologize profusely for the problems you had. I tested the software, but not nearly enough. I easily duplicated your error and found one other error:
    a. Your problem was caused by a cell having more than one font color (on my Notes Sheet - color me embarrassed).
    b. A similar problem occurred when a sheet was BLANK.

    I fixed both errors.

    See the attached corrected file that contains the following code (changes in red):
    Please Login or Register  to view this content.
    Thank you very much for the rep points.

    Lewis

  5. #5
    Forum Contributor
    Join Date
    08-20-2013
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Excel 2010, 2016, 2019
    Posts
    110

    Re: Set a Cell Background Color to Another Cell’s Color

    Hi Lewis,

    Thank you, all seems to be working fine, but some "headers" with a background colour doesn't seem to change. for example on sheet "Guest Manifest" R1 CA.

    It also doesn't change the background colour on new sheets.

    Is there not a way where the Macro can search for the default background colour (R0 | G36 | B52) in the entire work book and change to the new colour, even if the font is black?

    Workbook attached

    Thanks in advance.
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Set a Cell Background Color to Another Cell’s Color

    Hi,
    background colour doesn't seem to change. for example on sheet "Guest Manifest" R1 CA.
    Error was corrected. Those cells used formulas which required a slightly different search technique.

    Is there not a way where the Macro can search for the default background colour (R0 | G36 | B52) in the entire work book and change to the new colour, even if the font is black?
    Added per your request.

    It also doesn't change the background colour on new sheets.
    I don't understand this one. You have to run the Macro again when new sheets are created, since the Macro does not currently run automatically.

    Lewis

  7. #7
    Forum Contributor
    Join Date
    08-20-2013
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Excel 2010, 2016, 2019
    Posts
    110

    Re: Set a Cell Background Color to Another Cell’s Color

    You are a star!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to change cell colour, if the colour is based on value from formula?
    By darah237 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 04-19-2016, 11:40 AM
  2. Replies: 6
    Last Post: 04-11-2016, 09:48 AM
  3. Change colour of cells based on another cell's colour (Not value)
    By LTrain89 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-25-2013, 08:44 PM
  4. Replies: 2
    Last Post: 06-07-2011, 07:00 AM
  5. Colour change column chart based on cell colour
    By Alice21 in forum Excel General
    Replies: 11
    Last Post: 04-05-2011, 10:10 AM
  6. How to make row change colour based on text colour in row?
    By Joanna13 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-02-2010, 04:39 PM
  7. [SOLVED] change a cell background colour to my own RGB colour requirements
    By Stephen Doughty in forum Excel General
    Replies: 4
    Last Post: 06-16-2006, 08:15 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1