Results 1 to 6 of 6
  1. #1
    Lounger
    Join Date
    Dec 2009
    Location
    Downey Ca
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    If Statement Values and Changing Cell Colors Problem

    Here's what I need to do. Using an IF statement enter a number value in cell B8. Enter a number value in cell B28. If they are equal number change font to red. If they are not equal change number font to black. Can someone write it out for me. I have spent days trying to get this to work. Thanks in advance.

  2. #2
    5 Star Lounger
    Join Date
    Jan 2004
    Location
    Praha
    Posts
    988
    Thanks
    56
    Thanked 105 Times in 90 Posts
    The easiest way is to use Conditional Formatting.

    To set the font red in B28 when it equals the value in B8 you need to set Condition 1 to: Formula is . . . =$B$28=$B$8
    and choose red for the font.

    If you have the font of B28 normally set to black, it will default to that if the two cells are not equal in value.

  3. #3
    Lounger
    Join Date
    Dec 2009
    Location
    Downey Ca
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    How would this be typed out using an If statement in a Macro???

  4. #4
    5 Star Lounger
    Join Date
    Jan 2004
    Location
    Praha
    Posts
    988
    Thanks
    56
    Thanked 105 Times in 90 Posts
    Code:
    Range($B$28).Font.ColorIndex = 3
    Sets the cell font to red

    Code:
    Range($B$28).Font.ColorIndex = 0
    Sets the cell font to black

    So:

    Code:
    If [your test] Then Range($B$28).Font.ColorIndex = 3 Else Range($B$28).Font.ColorIndex = 0

  5. #5
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts
    Here is the code, though the conditional format would be "live" without the need of code.
    Code:
    If Range("B28") = Range("B8") Then
      Range("B28").Font.Color = vbRed
    Else
      Range("B28").Font.Color = vbBlack
    End If
    Steve

  6. #6
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts
    The range($B$28) needs to be a text. There is no need for the dollar signs:
    Range("B28")

    I prefer color over colorindex. The colorindex can be changed by the user so is not always the default value..
    Steve

Posting Permissions

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