VBA FormatConditions formatting bug maybe?

dt192

New Member
Joined
Jun 14, 2015
Messages
17
Hi all,

I have some VBA code that clears conditional formatting in the sheet, then adds three new rules.

I am experiencing a bug where the last added rule "A:K" does not apply the formatting, however if I move that section to the top, it then does apply the formatting, but the other two rules do not. If I manually set the formatting only in Conditional Formatting Rules Manager everything works fine.

Any ideas what could be going on? Also is there a better way to determine if the first row contains a date than =AND(ISNUMBER($A1),$A1>40000)?

Thanks in advance for your help.

VBA Code:
Public Function Format_cells(ByVal SheetName As String)
    With Sheets(SheetName).Range("A:K")
        .FormatConditions.Delete
    End With
    With Sheets(SheetName).Range("B:B")
        .FormatConditions.Add xlExpression, Formula1:="=COUNTIF(B:B,B1)>1"
        .FormatConditions(1).Interior.Color = RGB(255, 199, 206)
        .FormatConditions(1).Font.Color = RGB(156, 0, 6)
    End With
    With Sheets(SheetName).Range("H:H")
        .FormatConditions.Add xlExpression, Formula1:="=VALUE(H1)>1"
        .FormatConditions(1).Interior.Color = RGB(255, 199, 206)
        .FormatConditions(1).Font.Color = RGB(156, 0, 6)
    End With
        With Sheets(SheetName).Range("A:K")
        .FormatConditions.Add xlExpression, Formula1:="=AND(ISNUMBER($A1),$A1>40000)"
        .FormatConditions(1).Interior.Color = RGB(255, 199, 206)
        .FormatConditions(1).Font.Color = RGB(156, 0, 6)
    End With
End Function

excel.jpg
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Rewrite the function to use the formatcondition returned by the Add method:

VBA Code:
Public Function Format_cells(ByVal SheetName As String)
    With Sheets(SheetName)
        .Range("A:K").FormatConditions.Delete
        With .Range("B:B")
            With .FormatConditions.Add(xlExpression, Formula1:="=COUNTIF(B:B,B1)>1")
                .Interior.Color = RGB(255, 199, 206)
                .Font.Color = RGB(156, 0, 6)
            End With
        End With
        With .Range("H:H")
            With .FormatConditions.Add(xlExpression, Formula1:="=VALUE(H1)>1")
                .Interior.Color = RGB(255, 199, 206)
                .Font.Color = RGB(156, 0, 6)
            End With
        End With
        With .Range("A:K")
            With .FormatConditions.Add(xlExpression, Formula1:="=AND(ISNUMBER($A1),$A1>40000)")
                .Interior.Color = RGB(255, 199, 206)
                .Font.Color = RGB(156, 0, 6)
            End With
        End With
    End With
End Function
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,561
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top