you will find the issue with UDF is it doesnt react to changes made to your spreadsheet without "re-running" the code
i assume you know you can update by going into the cell with the formula press f2 and enter which is why you want something more "dynamic"

Function ColorFunction(rColor As Range, rRange As Range, Optional SUM As Boolean)
    Dim rCell As Range
    Dim lCol As Long
    Dim vResult As Long
    
    Application.Volatile True
        
    lCol = rColor.Interior.ColorIndex
    
    If SUM = True Then
        For Each rCell In rRange
            If rCell.Interior.ColorIndex = lCol Then
                vResult = WorksheetFunction.SUM(rCell, vResult)
            End If
        Next rCell
    Else
        For Each rCell In rRange
            If rCell.Interior.ColorIndex = lCol Then
                vResult = 1 + vResult
            End If
        Next rCell
    End If
    ColorFunction = vResult
End Function
adding in application.volatile will somewhat remedy this but be warned, it does degrade performance due to the constant running of the code..this becomes important if the size of your spreadsheet is large or the number of times you use this UDF

if its only something small or you dont use this UDF often you probably wont notice

ps even with this, you need to force update (F9) in order to update formula