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: 12
Last edited by a moderator:

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

Forum statistics

Threads
1,224,821
Messages
6,181,163
Members
453,021
Latest member
Justyna P

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