+ Reply to Thread
Results 1 to 3 of 3

WorksheetFunction.Sum() Not Equal to Range().Value

Hybrid View

  1. #1
    Registered User
    Join Date
    06-02-2015
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    2

    WorksheetFunction.Sum() Not Equal to Range().Value

    Hello all,

    I have this line of code:
    If WorksheetFunction.Sum(Range("G4:G11")) <> Range("I4").Value Then
    which is evaluating to true when I have these numbers:
    G4 = 495.00
    G5 = 221.18
    I4 = 716.18


    I ran this code to check that these were indeed equal:
            MsgBox Range("I4").Value
            MsgBox WorksheetFunction.Sum(Range("G4:G11"))
    both of these MsgBoxes returned 716.18.

    Anyone know why Excel doesn't think these are equal?

    Thanks,
    Nate

  2. #2
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,438

    Re: WorksheetFunction.Sum() Not Equal to Range().Value

    My first guess is that it is a rounding error (http://www.cpearson.com/Excel/rounding.htm ) I notice that =(I4-sum(G4:G5)) yields something like 1E-13 -- not exactly 0. Anytime you deal with arithmetic in a computer (this is not limited to Excel), you have the potential for round off error, and your programming needs to be prepared to handle it.

    Experienced programmers that I know never test for "exactly equal to" or "not equal to" in the way you are doing it here. They will account for this round off error in some way:

    Is abs(a-b)<epsilon where epsilon is some small value that represents the necessary precision.
    Is round(a,epsilon)<>round(b,epsilon) where epsilon, again, represents the level of precision that the test is required to pass.

    I expect that is the issue you are up against, and that you will need to adapt your code to account for this round off error.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  3. #3
    Registered User
    Join Date
    06-02-2015
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    2

    Re: WorksheetFunction.Sum() Not Equal to Range().Value

    Ok, yeah, I think you're right, I tried this:
    If Round(WorksheetFunction.Sum(Range("G4:G11")), 2) <> Round(Range("I4").Value, 2) Then
    and it seems to be working now. Thanks for the great article link too, makes much more sense now.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Having trouble with assigning a range in worksheetfunction.vlookup. help please!
    By kjy1989 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-22-2013, 04:35 AM
  2. Running a WorksheetFunction on a Range of Cells and returning results in 2nd Range
    By excelforum123 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-27-2013, 12:23 PM
  3. [SOLVED] trouble with named range in worksheetfunction.match
    By smartphreak in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-06-2013, 04:18 AM
  4. Replies: 3
    Last Post: 12-12-2012, 12:30 PM
  5. Range object in Worksheetfunction.Sum
    By Nuraq in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-21-2006, 09:10 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1