Results 1 to 5 of 5

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

Threaded View

  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%

    Private Sub Worksheet_Change(ByVal Target As Range)
       If Application.WorksheetFunction.Sum(Range("D6:D9")) <> 1 Then
            MsgBox "Note: Market shares for year do not sum to 100%, please check entries"
        End If
       If Application.WorksheetFunction.Sum(Range("E6:E9")) <> 1 Then
            MsgBox "Note: Market shares for year do not sum to 100%, please check entries"
        End If
       If Application.WorksheetFunction.Sum(Range("F6:F9")) <> 1 Then
            MsgBox "Note: Market shares for year do not sum to 100%, please check entries"
        End If
       If Application.WorksheetFunction.Sum(Range("G6:G9")) <> 1 Then
            MsgBox "Note: Market shares for year do not sum to 100%, please check entries"
        End If
       If Application.WorksheetFunction.Sum(Range("H6:H9")) <> 1 Then
            MsgBox "Note: Market shares for year do not sum to 100%, please check entries"
        End If
     If Application.WorksheetFunction.Sum(Range("K6:K9")) <> 1 Then
            MsgBox "Note: Market shares for year do not sum to 100%, please check entries"
        End If
    If Application.WorksheetFunction.Sum(Range("L6:L9")) <> 1 Then
            MsgBox "Note: Market shares for year do not sum to 100%, please check entries"
        End If
    If Application.WorksheetFunction.Sum(Range("M6:M9")) <> 1 Then
            MsgBox "Note: Market shares for year do not sum to 100%, please check entries"
        End If
    If Application.WorksheetFunction.Sum(Range("N6:N9")) <> 1 Then
            MsgBox "Note: Market shares for year do not sum to 100%, please check entries"
        End If
    If Application.WorksheetFunction.Sum(Range("O6:O9")) <> 1 Then
            MsgBox "Note: Market shares for year do not sum to 100%, please check entries"
        End If
    End Sub

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

    Private Sub Worksheet_Change(ByVal Target As Range)
    MsgBox Application.WorksheetFunction.Sum(Range("D6:D9")) = 1
    End Sub
    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.

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