Search code examples
excelvbavariablescount

VBA How to count variables with values


I have at least 6 different variables in my codes. 3 of them variants, 3 of them strings. I'd like to count the variants with values and count the strings with values ( like 2 variants, 3 strings ) and check to see if they are equal.

Any ideas?


Solution

  • Maybe something like:

    Sub Tester()
        Dim WA1, WA2, WA3
        Dim WS1 As String, WS2 As String, WS3 As String
    
        WA2 = Now
        WS3 = "then"
    
        If CountValues(WA1, WA2, WA3) = CountValues(WS1, WS2, WS3) Then
            Debug.Print "same!"
        End If
    
    End Sub
    
    'of the arguments passed, count how many have a value
    Function CountValues(ParamArray vals())
        Dim i As Long, v
        For Each v In vals
            If Len(v) > 0 Then i = i + 1
        Next v
        CountValues = i
    End Function