+ Reply to Thread
Results 1 to 8 of 8

Toggle cells background color based on value

  1. #1
    Registered User
    Join Date
    02-01-2022
    Location
    Vienna, Austria
    MS-Off Ver
    Office 365 / 2019
    Posts
    4

    Toggle cells background color based on value

    Hello,

    I have an Excel worksheet similar to this:

    Excel sample.jpg

    I would like the background color of the cells in column A to be toggled automatically between green and blue whenever the value changes. So that a newly inserted row gets automatically colored according to its value in column A. The objective is to use two colors and have consecutive equal entries in the same color as shown in the example.

    Can this be achieved without programming i.e. with formulas and/or conditional formatting?

  2. #2
    Spammer
    Join Date
    10-23-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2003, Office 365
    Posts
    1,237

    Re: Toggle cells background color based on value

    You can use a helper column (and then hide it if you want to). Lets say your helper column is Q. In Q2 put in the following formula;

    =IF(A2=A1,Q1,1-Q1)

    Dragging this down column Q should give you a column of 1s and 0s

    Then you can use conditional formatting; Highlight Column A (A2 down)

    First rule =$Q2=1 then pick your colour
    Add =$Q2=0 then pick your other colour.

    If you cannot or don't want to use a helper column, then colour all the cells in Column A (from A2 down) in one colour, highlight column A from A2 down and use this conditional format formula

    =MOD(SUMPRODUCT(--(($A$1:INDIRECT(ADDRESS(ROW()-1,1,3,1))=$A$2:INDIRECT(ADDRESS(ROW(),1,3,1)))=FALSE)),2)

    then choose another format colour
    Last edited by Croweater; 02-01-2022 at 08:49 PM.

  3. #3
    Registered User
    Join Date
    02-01-2022
    Location
    Vienna, Austria
    MS-Off Ver
    Office 365 / 2019
    Posts
    4

    Re: Toggle cells background color based on value

    Good idea with the helper column. Thank you very much!

  4. #4
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2405 (Windows 11 23H2 64-bit)
    Posts
    81,448

    Re: Toggle cells background color based on value

    Two CF rules for A2 (one each for green and blue):

    =AND(A2<>"",ISEVEN(SUMPRODUCT(1/COUNTIF($A$2:$A2,$A$2:$A2))))

    =AND(A2<>"",ISODD(SUMPRODUCT(1/COUNTIF($A$2:$A2,$A$2:$A2))))

    Applies to: $A$2:$A$5000 (don't try to apply it to $A:$A as it won't work correctly)

    Make the applies to range as long as you will ever need it to be.
    Attached Files Attached Files
    Last edited by AliGW; 02-02-2022 at 04:38 AM.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  5. #5
    Registered User
    Join Date
    02-01-2022
    Location
    Vienna, Austria
    MS-Off Ver
    Office 365 / 2019
    Posts
    4

    Re: Toggle cells background color based on value

    Brilliant variant! Unfortunately, it is quite computationally intensive and on my (4 year old) notebook and with my quite large worksheet it brings Excel to its knees. When I scroll down, the coloring of the fields in column A doesn't keep up in terms of speed. Working with the worksheet is also affected because Excel seems to be consuming a lot of resources in the background to calculate the formulas for the conditional formattings. It's obviously due to the fact that the computational effort for the SUMPRODUCT function increases sharply with the length of the array (= number of rows in the worksheet).

    Too bad, I actually find this variant (without the need for a helper column) very elegant and would have liked to use it.

  6. #6
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2405 (Windows 11 23H2 64-bit)
    Posts
    81,448

    Re: Toggle cells background color based on value

    You could halve the computational aspect by doing what was suggeseted by your other helper: apply one of the two colours manually and use just one rule for the other.

    It may be that a VBA solution would be more efficient in your circumstances, but I wouldn't be able to help with that.

    However, we did solve the issue for you as presented in your opening post.

  7. #7
    Registered User
    Join Date
    02-01-2022
    Location
    Vienna, Austria
    MS-Off Ver
    Office 365 / 2019
    Posts
    4

    Re: Toggle cells background color based on value

    Absolutely! You guys helped me a lot. Thank you again!

    Speaking of which: I completely overlooked Croweater's second suggestion (without the helper column). I think he added it later after I had already read his post.
    Because of the performance impact of the SUMPRODUCT function, I will probably stick to the first suggestion with the helper column. I've already built it into my worksheet and it does the job.
    Many thanks and greetings from Vienna to both of you!

  8. #8
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2405 (Windows 11 23H2 64-bit)
    Posts
    81,448

    Re: Toggle cells background color based on value

    No, he edited his post long before you read it - you can see the edit time bottom left of the post in question (a few minutes after he originally posted it).

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

    Also, if you have not already done so, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of all those who offered help.

+ 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. Sum Cells Based on Background Color
    By alferd324 in forum Excel General
    Replies: 14
    Last Post: 02-05-2020, 10:19 AM
  2. How to Sum Cells based on Background Color?
    By real20 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 01-23-2020, 01:34 PM
  3. [SOLVED] Sum cells based on background color
    By Punxatawny in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-03-2018, 05:23 PM
  4. sum cells based on background color
    By Krueger in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 07-27-2017, 11:12 AM
  5. How to add cells just based on their color background?
    By meirelesj in forum Excel General
    Replies: 7
    Last Post: 07-28-2015, 07:04 AM
  6. Change cell background color based on another cells background color
    By Queo in forum For Other Platforms(Mac, Google Docs, Mobile OS etc)
    Replies: 4
    Last Post: 06-10-2014, 05:28 AM
  7. [SOLVED] Color Index to sum up cells based on background color
    By jph89 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-16-2013, 03:23 PM

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