+ Reply to Thread
Results 1 to 17 of 17

Conditional Formatting with IF Statement

  1. #1
    Registered User
    Join Date
    11-18-2011
    Location
    CA,USA
    MS-Off Ver
    Excel 2007
    Posts
    9

    Conditional Formatting with IF Statement

    Hello all,

    I've been trying to get direction on how to use IF Statement with conditional formatting (or vice versa) but no luck.

    Value in A1 is selected from dropdown box (via Data Validation Tool)
    Value in A2 is populated by whatever is chosed in A1
    Value in A3 is any digit that falls between specified number from A2

    I need A3 to change colors; red if it is not within 2 values specified in A2; green if value is within specified value in A2.

    Any takers?

  2. #2
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: Conditional Formatting with IF Statement

    Hi Walt, welcome to the forum.

    If I understand you correctly, you have TWO values in A2? e.g. "10 to 20" or "10-20"?

    If that is the case, can you split it into two cells? We can probably work around it if you can't, but it would make the conditional formatting formula shorter.

    =AND(A3>=A2,A3<=B2)

    If A2 has "10-20" as a range, the CF formula would be something like:

    =AND(A3>=LEFT(A2,FIND("-",A2)-1)+0,A3<=MID(A2,FIND("-",A2)+1,LEN(A2))+0)

  3. #3
    Registered User
    Join Date
    11-18-2011
    Location
    CA,USA
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Conditional Formatting with IF Statement

    Hi Paul
    If I understand you correctly, you have TWO values in A2? e.g. "10 to 20" or "10-20"?
    -Four which is dependent on what is selected from A1.
    --IF A1 is 1st Plate, A2 is 15-20 ASF
    --IF A1 is 2nd Plate, A2 is 7-12 ASF
    --IF A1 is 3rd Plate, A2 is 20-25 ASF
    -IF A2 is 15-20 ASF, A3 is green when number entered in A3 is between 15-20. A3 is red if number entered in A3 is not between 15-20.
    Last edited by Paul; 11-18-2011 at 05:51 PM. Reason: Added quote tags for clarity

  4. #4
    Registered User
    Join Date
    11-18-2011
    Location
    CA,USA
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Conditional Formatting with IF Statement

    My GM's name is Paul too, an avid Excel user.

  5. #5
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: Conditional Formatting with IF Statement

    Ok, set the normal background color of cell A3 to red. Add the following Conditional Formatting formula to cell A3:

    =AND(A3>=LEFT(A2,FIND("-",A2)-1)+0,A3<=SUBSTITUTE(MID(A2,FIND("-",A2)+1,LEN(A2))," ASF","")+0)

    Set the conditional formatting Fill color to green. This way, if the cell is empty or does not fall in the range from A2, it will be red.

  6. #6
    Registered User
    Join Date
    11-18-2011
    Location
    CA,USA
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Conditional Formatting with IF Statement

    Thanks Paul.
    Will this work also when A2 is 20-25 ASF or when A2 is 15-20 ASF? Will this make A3 cell change color if value entered is within 20-25?

  7. #7
    Registered User
    Join Date
    11-18-2011
    Location
    CA,USA
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Conditional Formatting with IF Statement

    Hi Paul,

    Which "Rule Type" do I select so I can enter the formula you've provided?

  8. #8
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: Conditional Formatting with IF Statement

    Click "Conditional Formatting" toolbar button > click "New Rule..." > click "Use a formula to determine which cells to format"

    Enter the formula provided into the "Format values where this formula is true:" textbox below that. Click the "Format..." button to set the Font/Fill colors. Click OK twice to return to the worksheet.

  9. #9
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: Conditional Formatting with IF Statement

    The formula works for all conditions, as long as A2 is in the format "XX-YY ASF".

  10. #10
    Registered User
    Join Date
    11-18-2011
    Location
    CA,USA
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Conditional Formatting with IF Statement

    Did anyone ever tell you that you're a genius.
    How did you ever find out how to do that?

    And yes, it works perfectly. I think you may have just gotten me a promotion!!!!!

  11. #11
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: Conditional Formatting with IF Statement

    I tell myself every day that I'm a genius. Doesn't impress people at all.

    I learned a lot about Excel just by playing around, Googling, and by reading many of the amazing posts by folks who are far more genius than I. Many (if not most) of my fellow moderators are Microsoft MVP's in Excel (or should be if they aren't).

    I'm just glad I can help when possible.

  12. #12
    Registered User
    Join Date
    11-18-2011
    Location
    CA,USA
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Conditional Formatting with IF Statement

    Thanks for the help and Happy Thanksgiving/Holidays.

    $200 42" Sharp LCD 1080p 60hz at Best Buy on Black Friday.

  13. #13
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: Conditional Formatting with IF Statement

    Same to you, Walt.

    I don't plan on getting in line outside Best Buy at 6pm (or earlier) Thanksgiving evening in the small chance of getting a TV, though.

  14. #14
    Registered User
    Join Date
    11-18-2011
    Location
    CA,USA
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Conditional Formatting with IF Statement

    Hi Paul,

    I'm back with another scenario. I've been trying to understand your equation so I can modify it, but I can't get it. I'm trying to update your equation for a similar (but different) type scenario where an additional IF from another cell will be needed, but I need to know which set of characters is triggering what.
    Is there anyway you can break it down for me?

    Thanks,
    WalterV.

  15. #15
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: Conditional Formatting with IF Statement

    Original Formula:

    =AND(A3>=LEFT(A2,FIND("-",A2)-1)+0,A3<=SUBSTITUTE(MID(A2,FIND("-",A2)+1,LEN(A2))," ASF","")+0)

    The AND() function tests each of the conditions within and returns TRUE or FALSE. All must be TRUE in order for the entire function to be TRUE. In my AND formula above there are two conditions.

    First Condition: A3>=LEFT(A2,FIND("-",A2)-1)+0

    FIND("-",A2) finds the position of the first hyphen in cell A2. This hyphen will be to the right of your first number. It then subtracts 1 from that position to exclude the hyphen in the LEFT function.

    LEFT(A2,FIND("-",A2)-1)+0 uses that position-1 as the number of characters from the left you want to return. It then adds 0 to that "number" so it is converted to a real number and not just a text string representation of a number.

    =LEFT("10-15 ASF",FIND("-","10-15 ASF")-1)+0 becomes
    =LEFT("10-15 ASF",3-1)+0
    =LEFT("10-15 ASF",2)+0
    ="10"+0
    =10

    The condition test then compares A3 to that value, and if A3 is greater than or equal to that value, then the condition returns TRUE.

    Second condition: A3<=SUBSTITUTE(MID(A2,FIND("-",A2)+1,LEN(A2))," ASF","")+0

    The MID function, MID( string , start position , length ), also looks for the first hyphen (notice the same FIND function) but then ADDS one spot to that position so that it starts returning text from one position to the right of that hyphen. The LEN() function at the end tells it to return as many characters exist in that cell. (Even though we're not starting at the first character, if you tell the MID function to return 20 characters in a cell only containing 15, you will only get a max of 15 characters, not 15 characters plus five spaces.)

    =MID(A2,FIND("-",A2)+1,LEN(A2)) becomes
    =MID("10-15 ASF",FIND("-","10-15 ASF")+1,LEN("10-15 ASF")
    =MID("10-15 ASF",3+1,9)
    =MID("10-15 ASF",4,9)
    =15 ASF

    The MID function is then wrapped in a SUBSTITUTE function to eliminate the " ASF" portion of the string and just return the second number (15). SUBSTITUTE( original text , text to be replaced , text to replace with )

    SUBSTITUTE("15 ASF"," ASF","") means substitute the " ASF" in "15 ASF" with nothing (an empty string). We then add 0 to the remaining string, "15", to convert it to a numeric value (15).

    A3 is once again compared with this second value, and if A3 is less than or equal to the value TRUE is returned. If either condition fails (returns FALSE), the entire function will return FALSE and not apply the conditional formatting.

    Hopefully this helps you and others who are looking to do the same or similar tasks.

  16. #16
    Registered User
    Join Date
    11-18-2011
    Location
    CA,USA
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Conditional Formatting with IF Statement

    When do your classes start? I swear I need to attend one.
    This is very helpful. Thank you so much for the explanation. I'll send you actual calculator when I'm done with it.

    and...I decided not to go to Best Buy and wait in line at 6pm, I'm going to Fry's and wait in line at 7pm!!!!
    Happy Holidays,
    Walter V.
    Last edited by walt222; 11-22-2011 at 06:06 PM.

  17. #17
    Registered User
    Join Date
    11-23-2011
    Location
    Hurst, Tx
    MS-Off Ver
    Excel 2007
    Posts
    25

    Re: Conditional Formatting with IF Statement

    Quote Originally Posted by Paul View Post
    Original Formula:

    =AND(A3>=LEFT(A2,FIND("-",A2)-1)+0,A3<=SUBSTITUTE(MID(A2,FIND("-",A2)+1,LEN(A2))," ASF","")+0)

    The AND() function tests each of the conditions within and returns TRUE or FALSE. All must be TRUE in order for the entire function to be TRUE. In my AND formula above there are two conditions.

    First Condition: A3>=LEFT(A2,FIND("-",A2)-1)+0

    FIND("-",A2) finds the position of the first hyphen in cell A2. This hyphen will be to the right of your first number. It then subtracts 1 from that position to exclude the hyphen in the LEFT function.

    LEFT(A2,FIND("-",A2)-1)+0 uses that position-1 as the number of characters from the left you want to return. It then adds 0 to that "number" so it is converted to a real number and not just a text string representation of a number.

    =LEFT("10-15 ASF",FIND("-","10-15 ASF")-1)+0 becomes
    =LEFT("10-15 ASF",3-1)+0
    =LEFT("10-15 ASF",2)+0
    ="10"+0
    =10

    The condition test then compares A3 to that value, and if A3 is greater than or equal to that value, then the condition returns TRUE.

    Second condition: A3<=SUBSTITUTE(MID(A2,FIND("-",A2)+1,LEN(A2))," ASF","")+0

    The MID function, MID( string , start position , length ), also looks for the first hyphen (notice the same FIND function) but then ADDS one spot to that position so that it starts returning text from one position to the right of that hyphen. The LEN() function at the end tells it to return as many characters exist in that cell. (Even though we're not starting at the first character, if you tell the MID function to return 20 characters in a cell only containing 15, you will only get a max of 15 characters, not 15 characters plus five spaces.)

    =MID(A2,FIND("-",A2)+1,LEN(A2)) becomes
    =MID("10-15 ASF",FIND("-","10-15 ASF")+1,LEN("10-15 ASF")
    =MID("10-15 ASF",3+1,9)
    =MID("10-15 ASF",4,9)
    =15 ASF

    The MID function is then wrapped in a SUBSTITUTE function to eliminate the " ASF" portion of the string and just return the second number (15). SUBSTITUTE( original text , text to be replaced , text to replace with )

    SUBSTITUTE("15 ASF"," ASF","") means substitute the " ASF" in "15 ASF" with nothing (an empty string). We then add 0 to the remaining string, "15", to convert it to a numeric value (15).

    A3 is once again compared with this second value, and if A3 is less than or equal to the value TRUE is returned. If either condition fails (returns FALSE), the entire function will return FALSE and not apply the conditional formatting.

    Hopefully this helps you and others who are looking to do the same or similar tasks.
    Thanks! I read through the code and was a bit lost, but you explained that very well. If more people explained the code as you did wow amazing what people could learn!! I will benefit from your post just knowing how to separate data like that. I have had problems in the past and never figured it out!! Thanks for the instruction as well as the answer.

+ 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