August
August

Reputation: 530

VBA comparison of two variables

I'm trying to compare two variables in VBA/Excel, but VBA thinks they're not the same even though they are.

I have created two PivotTables and I'm checking whether the values in both of them are the same.

The code is:

Dim pvt As PivotTable
Set pvt = ActiveSheet.PivotTables(2)
For Each Pi In pvt.PivotFields("Filialas").PivotItems
    With Sheets("Composite").Range("D:D")
    Set c = .Find(Pi.Value, LookIn:=xlValues)
    If Not c Is Nothing Then
        If Pi.DataRange.Value = c.Offset(0, 1).Value Then
            c.Offset(0, 6).Value = Pi.DataRange.Value & " equals " & c.Offset(0, 1).Value
            Else
            c.Offset(0, 6).Value = Pi.DataRange.Value & " does not equal " & c.Offset(0, 1).Value
        End If
    End If
    End With
Next

And the result:

enter image description here

I am not a programmer, so I would really appreciate your thoughts.

Thanks!

Upvotes: 0

Views: 1520

Answers (1)

David
David

Reputation: 2065

The issue may be because the floats are larger than what they are appearing to be (E.g. it might be 61.812), and the program is auto rounding to two decimal places if it needs it.

Have you tried expanding the decimal places to compare the numbers to make sure they are exactly the same?

  • The solution was correct and rounding the values to two decimal places fixed the issue, as seen in the comments of the question.

Upvotes: 4

Related Questions