VBA format conditions evaluate differently from spreadsheet.

nicf

New Member
Joined
Jun 6, 2022
Messages
1
Office Version
  1. 2019
Platform
  1. Windows
I have applied formatting conditions to a range of cells on the spreadsheet as shown in the image.
I have VBA code which evaluates format conditions and outputs based on them.
The problem is that when the conditions are applied in this way the VBA code pulls the formatting based on the first cell in the range and thus it evaluates incorrectly. Is there a VBA workaround for this? I need to keep the formatting on the spreadsheet the same.
Example code:
VBA Code:
Private Function ConditionNo(ByVal rgeCell As Range) As Integer
On Error GoTo ErrHandler

Dim iconditionscount As Integer
Dim objFormatCondition As FormatCondition

For iconditionscount = 1 To rgeCell.FormatConditions.count
    Set objFormatCondition = rgeCell.FormatConditions(iconditionscount)

    If objFormatCondition.Type = xlExpression Then
        If Application.Evaluate(objFormatCondition.Formula1) Then
            ConditionNo = iconditionscount
            Exit Function
        End If
       
    ElseIf InStr(1, objFormatCondition.Formula1, "ISBLANK") = 2 Then
        If IsEmpty(rgeCell.value) Then
            ConditionNo = iconditionscount
            Exit Function
        End If
   
    ElseIf objFormatCondition.Type = xlCellValue Then
        Select Case objFormatCondition.Operator
            Case xlBetween: If Compare(rgeCell.value, ">=", objFormatCondition.Formula1) = True And _
                               Compare(rgeCell.value, "<=", objFormatCondition.Formula2) = True Then _
                               ConditionNo = iconditionscount
       
            Case xlNotBetween: If Compare(rgeCell.value, "<", objFormatCondition.Formula1) = True Or _
                               Compare(rgeCell.value, ">", objFormatCondition.Formula2) = True Then _
                               ConditionNo = iconditionscount
       
            Case xlGreater: If Compare(rgeCell.value, ">", objFormatCondition.Formula1) = True Then _
                               ConditionNo = iconditionscount
       
            Case xlEqual: If Compare(rgeCell.value, "=", objFormatCondition.Formula1) = True Then _
                               ConditionNo = iconditionscount
       
            Case xlGreaterEqual: If Compare(rgeCell.value, ">=", objFormatCondition.Formula1) = True Then _
                               ConditionNo = iconditionscount
       
            Case xlLess: If Compare(rgeCell.value, "<", objFormatCondition.Formula1) = True Then _
                               ConditionNo = iconditionscount
       
            Case xlLessEqual: If Compare(rgeCell.value, "<=", objFormatCondition.Formula1) = True Then _
                               ConditionNo = iconditionscount
       
            Case xlNotEqual: If Compare(rgeCell.value, "<>", objFormatCondition.Formula1) = True Then _
                               ConditionNo = iconditionscount
       
        End Select
        If ConditionNo > 0 Then Exit Function

    Else
        Debug.Print "Could not detect cell formula"
    End If
Next iconditionscount

Exit Function
   
ErrHandler:
    MsgBox "[System Error] in ConditionalFormatting.ConditionNo: " + Err.Description
    ConditionNo = -99
End Function
 

Attachments

  • formatcoditions_problem.PNG
    formatcoditions_problem.PNG
    26.8 KB · Views: 8
Last edited by a moderator:

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

Forum statistics

Threads
1,214,979
Messages
6,122,560
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