+ Reply to Thread
Results 1 to 9 of 9

Sum from the last Monday of the month previous

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-16-2012
    Location
    Latina, ITALY
    MS-Off Ver
    Excel 2010--2016
    Posts
    947

    Sum from the last Monday of the month previous

    Good evening, I would need the help of you experts to find the right function that sums the values ​​in column C from the last Monday of the month previous to finish the last Sunday of the month.
    In the attached file you will find the example:
    from June to July results = 35
    from July to August result = 28.
    Thanks as always for your help and a greeting.
    Attached Files Attached Files

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Sum from the last Monday of the month previous

    Suggest this user defined function

    Function CountSpecial(AnyCell As Range) As Long
    Dim MySheet As Worksheet
    Dim MySheetIndex As Long
    Dim LastMondayRow As Long
    Dim LastSundayRow As Long
    
    
    Set MySheet = AnyCell.Parent
    MySheetIndex = MySheet.Index
    For N = MySheet.Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1
            If WorksheetFunction.Weekday(MySheet.Cells(N, 1)) = 1 Then
                LastSundayRow = N
                Exit For
            End If
        Next N
    If MySheetIndex > 1 Then
        
        For N = MySheet.Parent.Sheets(MySheetIndex - 1).Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1
            If WorksheetFunction.Weekday(MySheet.Parent.Sheets(MySheetIndex - 1).Cells(N, 1)) = 2 Then
                LastMondayRow = N
                Exit For
            End If
        Next N
        CountSpecial = MySheet.Parent.Sheets(MySheetIndex - 1).Cells(Rows.Count, 1).End(xlUp).Row - LastMondayRow + LastSundayRow - 1
    
    Else
        LastMondayRow = 3
    End If
    End Function
    Open the VBA editor by hitting Alt F11.
    Insert a new module with Insert - Module
    Paste in the above function
    Go back to the sheet by hitting Alt F11.

    In a suitable cell, enter =CountSpecial(F20) or any other cell on the sheet - doesn't matter which

    Remember to save the workbook as a macro enabled workbook .xlsm
    Martin

  3. #3
    Forum Contributor
    Join Date
    05-16-2012
    Location
    Latina, ITALY
    MS-Off Ver
    Excel 2010--2016
    Posts
    947

    Re: Sum from the last Monday of the month previous

    MRICE Hello, thanks for your time & work.
    Unfortunately I tried your function and gives me different results.
    In July, returns 29 instead of 28 SUM (C3: C30) (July) +7 SUM (C26: C32) (June) = 35
    in August returns 28 instead of 25 SUM (C3: C27) (August) +3 SUM (C31: C33) (July) = 28
    Maybe I was not well explained that the column to be added is the C 1 are all now but you can also other numbers.
    Start the sum of column C from the last Monday of the month in column A, and you end up the sum, the last Sunday of the month following.

  4. #4
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Sum from the last Monday of the month previous

    OK

    New name for the function as it is summing

    Function SumSpecial(AnyCell As Range) As Long
    Dim MySheet As Worksheet
    Dim MySheetIndex As Long
    Dim LastMondayRow As Long
    Dim LastSundayRow As Long
    
    
    Set MySheet = AnyCell.Parent
    MySheetIndex = MySheet.Index
    For N = MySheet.Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1
            If WorksheetFunction.Weekday(MySheet.Cells(N, 1)) = 1 Then
                LastSundayRow = N
                Exit For
            End If
        Next N
    If MySheetIndex > 1 Then
        
        For N = MySheet.Parent.Sheets(MySheetIndex - 1).Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1
            If WorksheetFunction.Weekday(MySheet.Parent.Sheets(MySheetIndex - 1).Cells(N, 1)) = 2 Then
                LastMondayRow = N
                Exit For
            End If
        Next N
        SumSpecial = WorksheetFunction.Sum(MySheet.Parent.Sheets(MySheetIndex - 1).Range(MySheet.Parent.Sheets(MySheetIndex - 1).Cells(Rows.Count, 3).End(xlUp), MySheet.Parent.Sheets(MySheetIndex - 1).Cells(LastMondayRow, 3))) + WorksheetFunction.Sum(MySheet.Range(MySheet.Cells(3, 3), MySheet.Cells(LastSundayRow, 3)))
    
    Else
        LastMondayRow = 3
    End If
    SumSpecial = WorksheetFunction.Sum(MySheet.Range(MySheet.Cells(3, 3), MySheet.Cells(LastSundayRow, 3)))
    End Function
    I note that cell A33 on the sheet1 tab contains a value for a Monday even though it is not visible. You need to delete this to avoid confusing the formula.even

  5. #5
    Forum Contributor
    Join Date
    05-16-2012
    Location
    Latina, ITALY
    MS-Off Ver
    Excel 2010--2016
    Posts
    947

    Re: Sum from the last Monday of the month previous

    Hello there we are still in Sheet2 as a result of missing 28 + 7 of sheet1 from A26: A32 = 35 total
    In Sheet3 as a result of missing 25 +3 from Sheet2 A31: A33 = 28 total
    If at you from different outcome, send me the file please thanks.

  6. #6
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Sum from the last Monday of the month previous

    Please see the attached. I did find a bug where I had two lines in the wrong order.
    Attached Files Attached Files

  7. #7
    Forum Contributor
    Join Date
    05-16-2012
    Location
    Latina, ITALY
    MS-Off Ver
    Excel 2010--2016
    Posts
    947

    Re: Sum from the last Monday of the month previous

    Hello, we got almost to the solution. Now the totals are accurate.
    I have a problem if I change some value in C3 example Sheet2 I put 4 instead of 1 total expected to rise from 35 to 38, but does not do it automatically.
    I plugged in the bottom of the function: ActiveSheet.Calculate but does not work the same.
    Can anything be done?
    thanks again

  8. #8
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Sum from the last Monday of the month previous

    Try


    Application.Volatile
    immediately after the first line

  9. #9
    Forum Contributor
    Join Date
    05-16-2012
    Location
    Latina, ITALY
    MS-Off Ver
    Excel 2010--2016
    Posts
    947

    Re: Sum from the last Monday of the month previous

    Ok. perfect, everything works now.
    Thank you very much for your help.

+ 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. Replies: 3
    Last Post: 12-30-2012, 03:59 PM
  2. VBA - Date required for Monday & Sunday of week previous
    By wotadude in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-18-2011, 03:14 PM
  3. SOLVED - Display the date of the previous Monday?
    By tavlrb in forum Excel General
    Replies: 2
    Last Post: 02-26-2011, 01:50 AM
  4. [SOLVED] The Monday of the previous week with VBA
    By Gérard Ducouret in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-06-2006, 04:30 PM
  5. Replies: 1
    Last Post: 04-13-2005, 06: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