+ Reply to Thread
Results 1 to 12 of 12

If Then Syntax to Color Cells Depending on the Condition 'Value' - possible?

  1. #1
    Registered User
    Join Date
    02-18-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2011
    Posts
    7

    If Then Syntax to Color Cells Depending on the Condition 'Value' - possible?

    I am a VBA beginner and am struggling with the following:

    I have a series of 25 values (in this case, names) which I need to use as triggers to colour cells 25 different colours. Eventually I will have 100 worksheets that all need to have the same 25 'trigger values'. I have been trying to learn and use the If...Then and If...Then...Else syntax's to get this working and am struggling with even the most basic code (below). Nothing happens when I run the below marcos. Any help would be appreciated.

    Please Login or Register  to view this content.
    Last edited by ooos9272; 02-18-2013 at 08:00 PM. Reason: forgot to use the [CODE] and [/CODE]

  2. #2
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: If Then Syntax to Color Cells Depending on the Condition 'Value' - possible?

    Hi, as a new member, you should have looked at our rules. Specially at the one about quoting program code which MUST be enclosed between brackets. At the beginning of your code, you open a bracket [, you write CODE and you close the bracket ]. At the end of your code you open a bracket [, write /CODE and close the bracket ]. Then, you'll be all set to get an answer to your thread.
    Pierre Leclerc
    _______________________________________________________

    If you like the help you got,
    Click on the STAR "Add reputation" icon at the bottom.

  3. #3
    Registered User
    Join Date
    02-18-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2011
    Posts
    7

    Re: If Then Syntax to Color Cells Depending on the Condition 'Value' - possible?

    Thanks, I'll restart the thread

  4. #4
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: If Then Syntax to Color Cells Depending on the Condition 'Value' - possible?

    You should be able to modify it

  5. #5
    Registered User
    Join Date
    02-18-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2011
    Posts
    7

    Re: If Then Syntax to Color Cells Depending on the Condition 'Value' - possible?

    edited now, fingers crossed for help!

  6. #6
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: If Then Syntax to Color Cells Depending on the Condition 'Value' - possible?

    Here is your macro modified to work.
    Please Login or Register  to view this content.
    I can see that this is just the beginning.
    Hope this helps.

  7. #7
    Registered User
    Join Date
    02-18-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2011
    Posts
    7

    Re: If Then Syntax to Color Cells Depending on the Condition 'Value' - possible?

    Definitely a help. I have simply tried to increase the Range to cover multiple cells and am coming up with an error. Can anyone tell me why?

    Please Login or Register  to view this content.

  8. #8
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: If Then Syntax to Color Cells Depending on the Condition 'Value' - possible?

    Try this:
    Please Login or Register  to view this content.
    Last edited by p24leclerc; 02-19-2013 at 04:52 PM.

  9. #9
    Registered User
    Join Date
    02-18-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2011
    Posts
    7

    Re: If Then Syntax to Color Cells Depending on the Condition 'Value' - possible?

    Thanks for the help so far. I'm definitely getting somewhere with the code. I'm trying to use this same code on another worksheet (i will need to use it on 100 worksheets eventually) but when I try to do so I get an error, and the error states that I need to de-bug the following

    Please Login or Register  to view this content.
    My understanding was that if you don't specify which worksheet a code chain is for, then it works on whichever worksheet you have open...?

    Here is the full code I am currently using:

    Please Login or Register  to view this content.

  10. #10
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: If Then Syntax to Color Cells Depending on the Condition 'Value' - possible?

    Try this macro:
    Please Login or Register  to view this content.
    Change the range in the first FOR NEXT loop from A1:A19 to suit your application.
    This is a lot easier to manage and maintain.

    I saw those names in your list of names: samh, samg and sam. Be aware that "sam" will always be found in "samh" and "samg". If you place "sam" at the beginning of your array, it will not interfere with the other two. But at the end of the array, it will change the color of them both.

    If you only have those characters in your cells and that they are always written the same way (no capital), you can use this IF statement instead and it will make the difference between sam and samh or samg.
    Please Login or Register  to view this content.
    I hope this will help you
    Regards

  11. #11
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: If Then Syntax to Color Cells Depending on the Condition 'Value' - possible?

    Try this macro:
    Please Login or Register  to view this content.
    Change the range in the first FOR NEXT loop from A1:A19 to suit your application.
    This is a lot easier to manage and maintain.

    I saw those names in your list of names: samh, samg and sam. Be aware that "sam" will always be found in "samh" and "samg". If you place "sam" at the beginning of your array, it will not interfere with the other two. But at the end of the array, it will change the color of them both.

    If you only have those characters in your cells and that they are always written the same way (no capital), you can use this IF statement instead and it will make the difference between sam and samh or samg.
    Please Login or Register  to view this content.
    I hope this will help you
    Regards

  12. #12
    Registered User
    Join Date
    02-18-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2011
    Posts
    7

    Re: If Then Syntax to Color Cells Depending on the Condition 'Value' - possible?

    Thankyou again for your help, I worked around this problem by adding a button to each page - this seems to be doing the trick, and I did not need to change my code.

    Thankyou very much, I do appreciate all your help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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