Home » excel » excel – Conditional Formatting blank cells -VBA

excel – Conditional Formatting blank cells -VBA

Posted by: admin May 14, 2020 Leave a comment

Questions:

I need to highlight a cell in column B if it is less than value in column F. For example if cell B2 is 10 and F2 is 20, B2 should be red. However in column B there are blank cells i do not want these highlighted. For example B6 is blank but F6 is 10. In my code B6 become red as well.

Also how would i highlight a cell in the same row that is already highlighted. For example, if B2 is highlighted, highlight F2.

My code is below:

Sub threecf()
    Dim rg As Range
    Dim cond1 As FormatCondition, cond2 As FormatCondition
    Set rg = Range("B2", Range("B2").End(xlDown))

    'clear any existing conditional formatting
    rg.FormatConditions.Delete

    'define the rule for each conditional format
    Set cond1 = rg.FormatConditions.Add(xlCellValue, xlLess, "=f2")
    Set cond2 = rg.FormatConditions.Add(xlCellValue, xlEqual, "=isempty(f2)")

    'define the format applied for each conditional format
    With cond1
      .Interior.Color = vbRed
      .Font.Color = vbWhite
    End With

    With cond2
      .Interior.Color = vbWhite
      .Font.Color = vbWhite
    End With

End Sub
How to&Answers:

As mentioned in my comment, use formulas. No need to use VBA

Easiest Way (Recommended Way)

I recommend this way because it takes into account new rows that are being added.

  1. Select Col B
  2. Select Home Tab | Conditional formatting | New Rule | Use a formula to determine which cells to format
  3. Enter the formula =AND(B1<F1,B1<>"")
  4. Select Format | Fill Tab
  5. Set Fill color to red 🙂

Customized Way

  1. Manually select cells B2 to last row in col B
  2. Select Home Tab | Conditional formatting | New Rule | Use a formula to determine which cells to format
  3. Enter the formula =AND(B2<F2,B2<>"")
  4. Select Format | Fill Tab
  5. Set Fill color to red 🙂

VBA Way

If you still want VBA then try this

Sub Sample()
    Dim ws As Worksheet
    Dim lRow As Long

    '~~> Change as applicable
    Set ws = Sheet1

    With ws
        lRow = .Range("B" & .Rows.Count).End(xlUp).Row

        With .Range("B2:B" & lRow)
            .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(B2<F2,B2<>"""")"
            .FormatConditions(.FormatConditions.Count).SetFirstPriority

            With .FormatConditions(1).Interior
                .PatternColorIndex = xlAutomatic
                .Color = 255
                .TintAndShade = 0
            End With

            .FormatConditions(1).StopIfTrue = False
        End With
    End With
End Sub

Answer:

Go to conditional formatting –> New Rule –> Use a formula to determine which cells to format –> Paste This: =IF(AND(B2<F2,B2<>"") = TRUE,1,0)

For the F column: =IF(AND(F2>B2,F2<>"") = TRUE,1,0)

Answer:

If you want a VBA solution, try it without conditional formatting:

Sub StackOverflow()
    Dim x As Long
    With ThisWorkbook.Sheets("Stack")
        For x = 1 To .Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row
            If .Cells(x, 2).Value <> "" And .Cells(x, 2).Value < .Cells(x, 6).Value Then
                .Cells(x, 2).Interior.Color = vbRed
                .Cells(x, 6).Interior.Color = vbRed
                .Cells(x, 2).Font.Color = vbWhite
                .Cells(x, 6).Font.Color = vbWhite
            Else
                .Cells(x, 2).Interior.Pattern = xlNone
                .Cells(x, 6).Interior.Pattern = xlNone
                .Cells(x, 2).Font.Color = vbBlack
                .Cells(x, 6).Font.Color = vbBlack
            End If
        Next x
    End With
End Sub

Adapt the code to your needs (change the macro name, spreadsheet address and colors if you want).