0

I have a macro which applies conditional formatting to a row which highlights specific values I was wondering if there was a way to apply it to multiple rows (I want it to run from rows 18-79) As you can see from my code below, the function has to be adjusted for each row individually (I have done 3). I was wondering if there was an easier way to apply this rather than repeat and adjust for all the rows I need.

Sub Highlight()
'
' Highlight good values

Application.ScreenUpdating = False

    Dim ws As Worksheet


    For Each ws In ActiveWorkbook.Worksheets

    ws.Activate


    Rows("18:18").Select
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
        Formula1:="=$C$18", Formula2:="=$D$18"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Color = -16752384
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13561798
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False

    Rows("19:19").Select
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
        Formula1:="=$C$19", Formula2:="=$D$19"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Color = -16752384
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13561798
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False

    Rows("20:20").Select
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
        Formula1:="=$C$20", Formula2:="=$D$20"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Color = -16752384
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13561798
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False

    Next ws

Application.ScreenUpdating = True

End Sub

1 Answer 1

1

change the range to include it all:

With ActiveSheet.Rows("18:79")
    .FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
        Formula1:="=$C18", Formula2:="=$D18"
    .FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With .FormatConditions(1).Font
        .Color = -16752384
        .TintAndShade = 0
    End With
    With .FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13561798
        .TintAndShade = 0
    End With
    .FormatConditions(1).StopIfTrue = False
End With
Sign up to request clarification or add additional context in comments.

3 Comments

Each row has its own individual Formula e.g. for row 18 Formula1:="=$C18", Formula2:="=$D18" and for row 19 Formula1:="=$C19", Formula2:="=$D19"
@Zacchini did you try it, Conditional Formatting will iterate the relative references.
Ahh yes it works. I forgot to add end sub hence wasn't running

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.