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

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

Forum statistics

Threads
1,223,885
Messages
6,175,179
Members
452,615
Latest member
bogeys2birdies

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