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
Bookmarks