Consulting

Results 1 to 6 of 6

Thread: Adding Conditional Formatting using AND

  1. #1
    VBAX Contributor
    Joined
    Oct 2013
    Posts
    181
    Location

    Adding Conditional Formatting using AND

    I am needing to add some Conditional Formatting (CF) using VBA due to users cutting, pasting, and deleting cell and I end up with extra conditional formatting rules, I am trying to use the code below to add a CF to a worksheet but it keeps giving me an error at the formula. The formula works when added into the CF window.

    Any and all help would be appreciated.
    Thank you

    With Sheets("Sheet1").Range("$O$2:$O$500")
            '.FormatConditions.Delete
            With .FormatConditions.Add(Type:=xlExpression, Formula1:="=(AND(E2<>"",A2<>4,A2<>5,A2<>6,A2<>7,$O2<TODAY()+29)")
            .Interior.Color = vbYellow
            .Font.Italic = True
            .Font.Color = vbRed
      '      .Interior.Color = vbRed
      '      .Font.Color = vbWhite
      '      .Font.Bold = True
            .StopIfTrue = False
            End With
                    End With

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Quotes Chr(34) need to be doubled when inside a string

    So change

    Formula1:="=(AND(E2<>"",A2<>4,A2<>5,A2<>6,A2<>7,$O2<TODAY()+29)")

    to something like this and see

    Formula1:="=(AND(E2<>"""",A2<>4,A2<>5,A2<>6,A2<>7,$O2<TODAY()+29)")
    
    
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Contributor
    Joined
    Oct 2013
    Posts
    181
    Location
    I still receive an error Run-Time Error 5, Invalid Procedure Call or Argument!

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    This seems to work in my test WB

    Sub test()
        With Sheets("Sheet1").Range("$O$2:$O$500")
            .FormatConditions.Delete
        
            .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(E2<>"""", A2<>4,A2<>5,A2<>6,A2<>7,$O2<TODAY()+29)"
            
            With .FormatConditions(.FormatConditions.Count)
                .SetFirstPriority
                
                .Font.Bold = False
                .Font.Italic = True
                .Font.Color = vbRed
                .Interior.Color = vbYellow
        
                .StopIfTrue = False
            End With
        End With
                    
    End Sub

    I didn't notice it before but I think the parens after .Add and after 29)" were the issue


     With .FormatConditions.Add Type:=xlExpression, Formula1:="=(AND(E2<>"",A2<>4,A2<>5,A2<>6,A2<>7,$O2<TODAY()+29)"
    
    
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,874
    You don't need the opening bracket in the formula:
    With Sheets("Sheet4").Range("$O$2:$O$500")
      '.FormatConditions.Delete
      With .FormatConditions.Add(Type:=xlExpression, Formula1:="=AND(E2<>"""",A2<>4,A2<>5,A2<>6,A2<>7,$O2<TODAY()+29)")
        .Interior.Color = vbYellow
        .Font.Italic = True
        .Font.Color = vbRed
        '      .Interior.Color = vbRed
        '      .Font.Color = vbWhite
        '      .Font.Bold = True
        .StopIfTrue = False
      End With
    End With
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  6. #6
    VBAX Contributor
    Joined
    Oct 2013
    Posts
    181
    Location
    That worked, thank you for your help.

Posting Permissions

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