+ Reply to Thread
Results 1 to 10 of 10

Distinct colours in a cell and conditional formatting

  1. #1
    Registered User
    Join Date
    02-06-2010
    Location
    Brasília
    MS-Off Ver
    Excel 2007
    Posts
    23

    Distinct colours in a cell and conditional formatting

    This question about conditional formatting appears to be easy.
    Lets say I have a column like the following, like I posted somewhere else: (not very relevant for this)

    BGW
    3UG
    WWU
    1BR
    UUBRG
    WUBRG
    7UUGGRRR

    How do I have all characters in this column be coloured accordingly to:

    W = WHITE
    U = BLUE
    B = BLACK
    R = RED
    G = GREEN

    So that 7UUGGRRR would automatically switch to:
    7UUGGRRR

    What I already tried:
    - I applied conditional formatting to the column (actually, the cells interval, [C2..C8]) creating the rule: 'format only cells that contain -> specific text' -> then I added "U" as the text and told it to colour blue. As I predicted, this didn't work; the entire cell containing a single "U" will get blue, which is unwanted.

    Thanks.

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Distinct colours in a cell and conditional formatting question

    If you wish to format on a char by char on an automated basis you will need to use VBA.

    Let us know if that's a route you want to go down and if so, how the strings are populated - ie manually entered or generated via formulae etc...

  3. #3
    Registered User
    Join Date
    02-06-2010
    Location
    Brasília
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Distinct colours in a cell and conditional formatting question

    God, that was fast!
    Oh no.. I know absolutely nothing about VBA. All I know is that stands for Visual Basic for Applications (or Village Bars Association, I'm not sure :P).
    So if you're talking about a 'routine' then I don't have a clue of how to write one and how to implement.

    By the way, just curious: does VBA require a special application to be carried away, or can things be written in the good and old notepad?

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Distinct colours in a cell and conditional formatting question

    VBA is 99.9% of the time written into the VB Editor (ALT + F11 from native XL)

    We can help you with the code it was more a case of

    a) do you want to use VBA ?

    b) are you able to use VBA in your environment ?


    Point b) is more an issue in network environments or where the files are to be distributed.
    In most companies enabling VBA is not standard practice and on that basis requires intervention on the part of the user (at least once).
    On that basis accounting for the possibility that VBA may not be enabled is normally worthy of consideration on the part of the "designer"

    The only other thing worth noting is that Mac 2008 has no VBA support which may / may not be a concern.

  5. #5
    Registered User
    Join Date
    02-06-2010
    Location
    Brasília
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Distinct colours in a cell and conditional formatting question

    VBA is 99.9% of the time written into the VB Editor (ALT + F11 from native XL)
    OMG, I haven't heard of that "easter egg" in my life inside Excel! Ok, I opened VBA. It appears to have a nice interface, I just need some time to get used to it.

    We can help you with the code it was more a case of

    a) do you want to use VBA ?

    b) are you able to use VBA in your environment ?

    Point b) is more an issue in network environments or where the files are to be distributed.
    In most companies enabling VBA is not standard practice and on that basis requires intervention on the part of the user (at least once).
    On that basis accounting for the possibility that VBA may not be enabled is normally worthy of consideration on the part of the "designer"
    Well, I'd like to learn, as long as it's not so complicated (is it)? I'm completely unaware of the syntax, powers and limitations of this language.

    About b) maybe yes, this is a home computer. There are no company policies in here.

    The only other thing worth noting is that Mac 2008 has no VBA support which may / may not be a concern.
    No Macs in here, assuming you're talking about Microsoft Office 2008 for Mac. I'm a PC user.

    Sorry if I got something completely out of loop.

    And thanks so far!

  6. #6
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Distinct colours in a cell and conditional formatting

    Quote Originally Posted by Rinocerinho
    Well, I'd like to learn, as long as it's not so complicated (is it)? I'm completely unaware of the syntax, powers and limitations of this language.
    Excel in it's own right is pretty vast so it follows that VBA follows suit.

    I wouldn't say VBA is overly hard to get started with ... VBA is in itself a subset of VB and a lot of non-programmers find VB easier to follow / read than say C.

    At the same time though VBA is not something you will become an expert in within a few days / weeks.
    I've been coding (poorly) for the best part of ten years and what I don't know still far exceeds what I do know.

    There are many advantages to learning VBA - from being able to reduce repetitive taks to nothing more than a press of a button to the more advanced things like being able to control other MS Apps remotely (ie control Outlook from Excel and vice versa)

    However, it's often easy to get distracted with VBA and lose focus on the fact that the need for VBA can often be circumvented by good design in the first instance...

    That said with VBA at your fingertips there really is very little you can't do with XL (rightly / wrongly).

    If you search the forum for VBA Books etc you will find lots of good advice though don't lose sight of the fact that the Macro Recorder is perhaps the best learning tool available to you - ie press record - do what you want - stop recording - review the generated code... it might contain lots of superfluous info. but it will give the basic pointers in terms of syntax etc...


    Getting back to the specifics of your question...

    Where are the values/strings stored ? (you mentioned C2:C8 earlier, anywhere else ?)

    How are they entered ? (manually or result of formulae)

  7. #7
    Registered User
    Join Date
    02-06-2010
    Location
    Brasília
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Distinct colours in a cell and conditional formatting

    Hmm, I'll consider that. I'll read some books my friend can lend me. Thanks for the tips.

    About the data like:
    BGW
    3UG
    WWU
    1BR
    UUBRG
    WUBRG
    7UUGGRRR
    ...
    and so on: they're entered manually in a single column in a spreadsheet. They're not result of any formula.

    But.... are you sure this can only be accomplished with VBA?

  8. #8
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Distinct colours in a cell and conditional formatting

    Quote Originally Posted by rinocerinho
    are you sure this can only be accomplished with VBA?
    If you're happy to manually set the font of each character in the string, no.
    If you wish to automate the process, yes.
    Conditional Formatting, Custom Formatting are applied at a cell level rather than at an individual character level within the cell itself.

    In terms of a VBA process...

    Please Login or Register  to view this content.

    To use the above, right click on the tab name containing the values of interest, select View Code and paste the above into the resulting window.
    Thereafter, in a Macro enabled file, you will find that altering the values in Column A will lead to them being formatted as per your wishes.
    I would state that formatting font as white is obviously risky unless you have modified the background fill colour (to something other than those colours used above).

    You will need to save the file in a macro supported type - eg .xlsm ... or if you want to run in Compatibility Mode save as old .xls

    Modify ranges etc to suit your own requirements... the above is meant purely as an example.
    Last edited by DonkeyOte; 02-10-2010 at 03:10 AM. Reason: added note re: file type

  9. #9
    Registered User
    Join Date
    02-06-2010
    Location
    Brasília
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Distinct colours in a cell and conditional formatting

    To use the above, right click on the tab name containing the values of interest, select View Code and paste the above into the resulting window.
    Thereafter, in a Macro enabled file, you will find that altering the values in Column A will lead to them being formatted as per your wishes.
    I would state that formatting font as white is obviously risky unless you have modified the background fill colour (to something other than those colours used above).

    You will need to save the file in a macro supported type - eg .xlsm ... or if you want to run in Compatibility Mode save as old .xls

    Modify ranges etc to suit your own requirements... the above is meant purely as an example.
    Err, I saved the sheet as macro enabled, and I'm ready to paste the code, but what exactly did you mean by "tab name containing the values of interest"?

  10. #10
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Distinct colours in a cell and conditional formatting

    The sheet into which you'll be entering the values you intend to have colour coded.

    Assume that is Sheet1... in native XL right click on the tab Sheet1 and select View Code - paste the code provided into the resulting window.

    Once in place this means that whenever you enter value(s) into Column A (adjust code to suit) the values will be colour coded on a character by character basis as and where they meet your criteria.

    NOTE:
    per my point on your other post regards UpperCase conversion ... if that forms part of this exercise you will need to merge the two Change routines as a given sheet object (eg Sheet1) can have only one Change event.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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