+ Reply to Thread
Results 1 to 3 of 3

VBA compare sums in ranges and display delta in MsgBox

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-03-2014
    Location
    Berlin
    MS-Off Ver
    Excel 2010
    Posts
    160

    VBA compare sums in ranges and display delta in MsgBox

    Hey everybody,

    I am trying to compare the total sum from tab “Upload” column “P” (pls see attachment) with sums from tab “Master” in range J47:U59

    While column “P” in tab “Upload” stays fixed, the sum to be compared against in tab “Master” will vary in accordance with the entry in cell C6 in tab “Ctr”.
    Therefore, what the code should do is to compare the period in range J6:U6 in tab "Master" with the entry in cell C6 in tab “Ctr” and evaluate the sum of the corresponding month in range J47:U59 against the sum in column “P” tab “Upload”.

    For example:
    When the entry in Cell C6 tab “Ctr” = 01.05.2015 (format: May 2015), then the corresponding month in tab “Master” is 01.05.2015 (format: Mai). Therefore the sums to be compared are 85.200.710,81 (range N47:N59 tab “Master”) against 85.200.710,81 (range column “P” tab “Upload”)

    In this case the code should display in a msgbox something like “No delta detected in the reporting month - May 2015 –“.

    If no delta was detected, then the msgbox should display something like “Delta detected in the reporting month - May 2015 – Delta: #,##0.00”

    I've been trying to do that on my own but I wasn't successful.

    Therefore, your support is highly appreciated. Thanks a lot in advance!

    regards
    Attached Files Attached Files

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,873

    Re: VBA compare sums in ranges and display delta in MsgBox

    try such code in standard module (Alt+F11 Insert Module):
    Sub sums_compare()
    Dim sumupload As Currency, summaster As Currency, currentcolumn As Long
    With Application.WorksheetFunction
      sumupload = .Sum(Sheets("Upload").Columns("P:P"))
      currentcolumn = .Match(Sheets("Ctr").Range("C6"), Sheets("Master").Rows("6:6"), 0)
      summaster = .Sum(Sheets("Master").Cells(47, currentcolumn).Resize(13, 1))
      If summaster - sumupload = 0 Then
        MsgBox "Agreed for " & Format(Sheets("Ctr").Range("C6"), "mmm yy")
      Else
        MsgBox "For " & Format(Sheets("Ctr").Range("C6"), "mmm yy") & " difference is: " & summaster - sumupload
      End If
    End With
    End Sub
    As you can see these are basically excel formulas just called from within the code.

    if you want it to pop-up automatically after changing C6 in Ctr then in sheet code (right click on the sheet name tab and edit code) insert:
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Range("C6"), Target) Is Nothing Then
      Call sums_compare
    End If
    End Sub
    Best Regards,

    Kaper

  3. #3
    Forum Contributor
    Join Date
    04-03-2014
    Location
    Berlin
    MS-Off Ver
    Excel 2010
    Posts
    160

    Re: VBA compare sums in ranges and display delta in MsgBox

    Kaper,

    it works like magic!

    thanks a lot!

+ 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. Compare range with previous ranges,display elapsed row match
    By sans in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-19-2013, 12:08 PM
  2. Replies: 13
    Last Post: 03-04-2013, 04:25 PM
  3. ranges and sums of two different columns
    By BellaVegas in forum Excel General
    Replies: 1
    Last Post: 04-25-2012, 02:11 PM
  4. Replies: 5
    Last Post: 11-05-2011, 07:34 PM
  5. Sums With Ranges
    By [email protected] in forum Excel General
    Replies: 5
    Last Post: 05-19-2006, 12:35 PM

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