简体   繁体   中英

Conditional formatting to highlight specific cells but not empty and text cells VBA

I have a macro which highlights cells outside a range. The only problem with it, is that it also highlights all empty cells and cells with text. Is there a way for it to ignore these?

在此处输入图片说明

Here is my code

Sub Highlight()
'
' Highlight good values

Application.ScreenUpdating = False

    Dim ws As Worksheet


    For Each ws In ActiveWorkbook.Worksheets

    ws.Activate

        With ActiveSheet.Rows("18:79")
            .FormatConditions.Add Type:=xlCellValue, Operator:=xlNotBetween, _
                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

    Next ws

Application.ScreenUpdating = True

End Sub

You currently only have a row limitation to the range you are applying the conditional formatting to. If you want to limit the impacted range you just need to change your With to have both a Row and a Column limitation.


Update This:

With ActiveSheet.Rows("18:79")

To This:

With ActiveSheet.Range("A18:O79")

Edit

If each sheet has the SAME row range (18:79) but the columns have a VARYING range you just need to create a last column variable to create your dynamic range

Sub Highlight()

Dim ws As Worksheet, LC As Long

For Each ws In ActiveWorkbook.Worksheets
    LC = ws.Cells(18, ws.Columns.Count).End(xlToLeft).Column

    With ws.Range(ws.Cells(18, 1), ws.Cells(79, LC))
       'Formatting goes here
    End With

Next ws

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM