简体   繁体   中英

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:

在此处输入图片说明

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

Thanks!

The issue may be because the floats are larger than what they are appearing to be (Eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM