+ Reply to Thread
Results 1 to 5 of 5

VBA code: MsgBox if a range of cells do not sum to 100%

  1. #1
    Registered User
    Join Date
    07-28-2010
    Location
    london, england
    MS-Off Ver
    Excel 2007
    Posts
    91

    VBA code: MsgBox if a range of cells do not sum to 100%

    Hi all,

    I have the following code to display a message box if a range doesn't sum to 100%

    Please Login or Register  to view this content.

    However the msgbox appears even if the cells do sum to 100. I have tested all the ranges using the following code:

    Please Login or Register  to view this content.
    and found that the problem is in the range D6:D9. This range returns true as expected in most cases, but occasionally false even if the numbers sum to 100%.

    i.e. 5% 5%
    65% 41%
    13% 43%
    17% sums to 100% and returns true 11% sums to 100% but returns false.

    Could anyone provide any ideas as to why this might be happening?

    Thanks in advance,
    James
    Last edited by j.farr3ll; 09-05-2011 at 06:09 AM.

  2. #2
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: VBA code: MsgBox if a range of cells do not sum to 100%

    Please edit your post to include code tags around your code.

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  3. #3
    Registered User
    Join Date
    05-13-2011
    Location
    New Zealander in London
    MS-Off Ver
    Excel 2007
    Posts
    74

    Re: VBA code: MsgBox if a range of cells do not sum to 100%

    Right now I can tell you why this is happening, but haven't figured out the solution just yet.

    It is to do with how Excel stores the numbers when you reference them. By default Excel is reading the values as Integers, so the percentages are somehow being stored differently and the values 0.05, 0.41, 0.43 and 0.11 actually add up to 0.89.

    The way I could tell this is by using the following code and changing the variable type from As Integer to Double, Single, Variant, etc and seeing what the result is.

    I need to perform a bit more research into the correct way to store and read integers (as obviously the Integer variable type doesn't perform very well) to see how to fix this for you.

    Please Login or Register  to view this content.

  4. #4
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: VBA code: MsgBox if a range of cells do not sum to 100%

    I think something like this should fix it:

    Please Login or Register  to view this content.

    Dom

  5. #5
    Registered User
    Join Date
    07-28-2010
    Location
    london, england
    MS-Off Ver
    Excel 2007
    Posts
    91

    Re: VBA code: MsgBox if a range of cells do not sum to 100%

    Thanks for your help on this guys, understanding the integers gives me at least an idea of why this is happening.

    Dom the code you post above solves the issue. Am I right in thinking this just makes excel round to 2 decimal places?

    Cheers,
    James

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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