Results 1 to 7 of 7

Thread: VBA Code for Highlighting Cells based on If then Rules

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #6
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    443
    Location
    Okay, here is a possible approach showing the first 2 rules.
    Private Sub Worksheet_Change(ByVal Target As Range)
    SetHighlight
    End Sub
    
    
    Sub SetHighlight()
    Dim r As Integer, rCount As Integer
    rCount = Range("A" & Rows.Count).End(xlUp).Row
    For r = 2 To rCount
        Macro2 r, 16
        Macro2 r, 42
        Macro2 r, 43
        Macro2 r, 44
        If Cells(r, 16).Value = "Good" And Cells(r, 42).Value = 0 And Cells(r, 43).Value <= 10 Then
            Macro1 r, 16
            Macro1 r, 42
            Macro1 r, 43
        End If
        If Cells(r, 16).Value = "Good" And Cells(r, 42).Value = 1 And Cells(r, 44).Value <= 10 Then
            Macro1 r, 16
            Macro1 r, 42
            Macro1 r, 44
        End If
    Next
    End Sub
    
    
    Sub Macro1(r As Integer, c As Integer)
        With Cells(r, c).Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 65535
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
        Range("P2").Select
    End Sub
    
    
    Sub Macro2(r As Integer, c As Integer)
        With Cells(r, c).Interior
            .Pattern = xlNone
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
    End Sub
    Last edited by June7; 11-19-2022 at 01:06 AM.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Posting Permissions

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