Solved

Total worksheets based on Date

Posted on 2013-06-28
7
314 Views
Last Modified: 2013-07-02
I've named all worksheets in a "dd-mmm-yyyy" format
there is a section in within every worksheet that I want to do a YTD total on.  The total should be the current sheet as well as all worksheets within the current year less than the date of the current worksheet. It's kinda hard to explain.
I've Attached a copy of my worksheet.  the area that needs to be YTD is in Orange.
YTD-Totals.xlsx
0
Comment
Question by:Jenedge73
7 Comments
 
LVL 8

Expert Comment

by:itjockey
ID: 39285362
Hi Jenedge73,

will you pls explain in brief what to total. I am on sheet 6-apr2013 cell C17. in that Cell you want to add from this sheet as well as from other sheets.

I understood you want total for 6apr2013 + date older then this. correct me if I am wrong.


Thanks
0
 

Author Comment

by:Jenedge73
ID: 39285567
I want it to sum all c35 in worksheets with a date less that the current worksheet date in C17.
does that help?
0
 
LVL 8

Expert Comment

by:itjockey
ID: 39285783
Hi Jenedge73,

it required VBA Coding  as per my understanding & I don't do VBA, so can click on request attention button if you want to.




Thanks & Good Luck
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 2

Expert Comment

by:arildj78
ID: 39286091
Here is a working code for your problem. It should be pasted in ThisWorkbook section of VBA. When you run SumItAll, all values from C17 to S32 on all sheets with a valid date as a name will be set to $0.00.

Then each sheet with a valid date will be compared to the other and the appropriate values will be added one at a time. This is not the most effective way of doing this, so the calculation should not be done each time Excel does an automatic recalculation. Also, make sure that each person stays on the same column from sheet to sheet, as there are no checking of the names in row 3.

Finally, it's getting late, so I have not had time to debug this code thoroughly. Use it at your own risk and to control to see that the numbers really add up.
Sub SumItAll()
    Const NumbersOfDataRows = 16
    Const FirstSumRow = 17
    Const FirstSourceRow = 35
    Const FirstColumnToSum = 3
    Const LastColumnToSum = 19
    
    Dim CurrentSheet As Worksheet
    Dim DataSheet As Worksheet
    
    Dim CurrentDate As Date
    Dim DataDate As Date
    
    Dim ValidDate As Boolean
    
    Dim SplitDate As Variant
    
    For Each CurrentSheet In ThisWorkbook.Sheets
        ValidDate = SheetName2Date(CurrentSheet.Name, CurrentDate)
        If ValidDate Then
            'Zero the totals
            For r = 1 To NumbersOfDataRows
                For c = FirstColumnToSum To LastColumnToSum
                    CurrentSheet.Cells(r + FirstSumRow - 1, c).Value = 0
                Next c
            Next r
            
            For Each DataSheet In ThisWorkbook.Sheets
                ValidDate = SheetName2Date(DataSheet.Name, DataDate)
                If ValidDate Then
                    
                    'Dates on both sheets have been found valid
                    If (DataDate <= CurrentDate) And (DataDate >= DateSerial(Year(CurrentDate), 1, 1)) Then
                        'Date on DataSheet is within interesting range
                        For r = 1 To NumbersOfDataRows
                            For c = FirstColumnToSum To LastColumnToSum
                                CurrentSheet.Cells(r + FirstSumRow - 1, c).Value = _
                                CurrentSheet.Cells(r + FirstSumRow - 1, c).Value + _
                                DataSheet.Cells(r + FirstSourceRow - 1, c).Value
                            Next c
                        Next r
                        
                    End If
                End If
            Next DataSheet
        
        End If
    Next CurrentSheet
    
End Sub

Function SheetName2Date(ByRef SheetName As String, ByRef SheetDate As Date) As Boolean
    On Error GoTo errorhandler

    Dim dd As Integer
    Dim mm As Integer
    Dim yyyy As Integer
    Dim SplitDate As Variant
    
    SplitDate = Split(SheetName, "-")
    
    dd = SplitDate(0)
    
    Select Case SplitDate(1)
        Case "Jan": mm = 1
        Case "Feb": mm = 2
        Case "Mar": mm = 3
        Case "Apr": mm = 4
        Case "May": mm = 5
        Case "Jun": mm = 6
        Case "Jul": mm = 7
        Case "Aug": mm = 8
        Case "Sep": mm = 9
        Case "Oct": mm = 10
        Case "Nov": mm = 11
        Case "Dec": mm = 12
    End Select
    
    yyyy = SplitDate(2)
        
    SheetDate = DateSerial(yyyy, mm, dd)
    SheetName2Date = True
    
    Exit Function
    
errorhandler:
    On Error GoTo 0
    SheetName2Date = False
End Function

Open in new window

0
 
LVL 81

Accepted Solution

by:
byundt earned 500 total points
ID: 39286093
You might consider using a user-defined function with a worksheet formula like:
=YTD(C35)

Put the following code in a regular module sheet:
Function YTD(CellToSum As Range) As Variant
Dim ws As Worksheet
Dim CurrentDate As Date
Application.Volatile
CurrentDate = CDate(CellToSum.Worksheet.Name)
If IsDate(CellToSum.Worksheet.Name) Then
    CurrentDate = CellToSum.Worksheet.Name
Else
    YTD = "#DATE!"
    Exit Function
End If
For Each ws In CellToSum.Worksheet.Parent.Worksheets
    If IsDate(ws.Name) Then
        If CDate(ws.Name) <= CurrentDate Then YTD = Application.Sum(YTD, ws.Range(CellToSum.Address))
    End If
Next
End Function

Open in new window


The attached workbook shows how it might work. See the formula in cell C17 on 6-Apr-2013 worksheet.

Brad
YTD-TotalsQ28170943.xlsm
0
 

Author Closing Comment

by:Jenedge73
ID: 39288447
Fantastic Thanks
0
 
LVL 8

Expert Comment

by:itjockey
ID: 39293604
Mr. byundt,

what if I don't want to go for date based sum i.e. sum of all sheet cell excluding current sheet.

=YTD(C3) which calculate sum of all sheet excluding current sheet.

Cell Sum

Thanks
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VBA modules import 4 53
Excel 2013 Issues 11 41
remove extra space at end of cell 12 31
excel conditional formating 4 14
INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question