Results 1 to 3 of 3

Thread: [RESOLVED] VBA Excel - Double Comparison failure

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Location
    Best Place on Earth
    Posts
    363

    Resolved [RESOLVED] VBA Excel - Double Comparison failure

    Hi,

    I am trying to compare two values from different spreadsheets in a macro in Excel (Excel 2002, Visual Basic 6.3).

    Most of the time the comparison works, but occasionally it decides two values are unequal, when they are equal
    as far as I can see, and verify when stepping through the code in the Visual Basic Editor.

    The code is as follows

    VB Code:
    1. CheckGloss = Workbooks(SecurityWBK).ActiveSheet.Cells(GlossRow, GlossCoupon).Value
    2.             CheckSummit = SummitCouponRate
    3.             If CheckGloss <> CheckSummit Then
    4.                 Script
    5.             End If

    CheckGloss and CheckSummit are declared as Doubles.
    I resorted to using them as the code was failing on this comparison.

    SummitCouponRate is a Function, as follows

    VB Code:
    1. Function SummitCouponRate() As Double
    2.     If Trim(Workbooks(SummitWBK).ActiveSheet.Cells(SummitRow, SumFixFloat).Value) = "FIX" Then
    3.         SummitCouponRate = Workbooks(SummitWBK).ActiveSheet.Cells(SummitRow, SumCouponorSpread).Value
    4.     Else
    5.         SummitCouponRate = (Workbooks(SummitWBK).ActiveSheet.Cells(SummitRow, SumCouponorSpread).Value / 100#) + Workbooks(SummitWBK).ActiveSheet.Cells(SummitRow, SumCurrentRate).Value
    6.     End If
    7. End Function

    On most occasions where CheckGloss is equal to CheckSummit they are working fine, it is just a few where they are being taken to be not equal.

    CheckGloss is equal to 2.688

    Workbooks(SummitWBK).ActiveSheet.Cells(SummitRow, SumCouponorSpread).Value is 4.5
    Workbooks(SummitWBK).ActiveSheet.Cells(SummitRow, SumCurrentRate).Value is 2.643

    I have run the math through a calculator, and the fallback of pencil and paper and I still get 2.688 for CheckSummit.

    Does anyone have any ideas as to why this is happening, also what the solution is?
    Signature Under Construction

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: VBA Excel - Double Comparison failure

    Welcome to VBForums!

    The problem here is that you are using Doubles - unfortunately they are not accurate.

    One way around this is to round the numbers to a level that you are happy with (as the lack of accuracy usually occurs "late" after the decimal point), eg:
    VB Code:
    1. If Round(CheckGloss,3) <> Round(CheckSummit,3) Then

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Location
    Best Place on Earth
    Posts
    363

    Resolved Re: VBA Excel - Double Comparison failure

    Thanks,

    I had not realised that Doubles had that limitation.

    Unfortunately I need full accuracy on the numbers, and I cannot tell what precision it will be working to.

    I tried changing the Declaration of CheckGloss and CheckSummit to Strings and the Macro now works fine.
    Signature Under Construction

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width