+ Reply to Thread
Results 1 to 15 of 15

Help with making a cumulative timesheet automated

Hybrid View

  1. #1
    Registered User
    Join Date
    07-21-2014
    Location
    San Diego
    MS-Off Ver
    Office 2010
    Posts
    8

    Help with making a cumulative timesheet automated

    I have a task from my boss that would help him out a lot. I attached a dummy spreadsheet so you can see what I'm talking about. What I need to do is have a different tab for every month and then the "totals" tab will automatically update with the total amount of hours each person worked on each project and phase. When a new month is added, I want it to be able to account for this automatically (or with very little change to the code) and update the total spreadsheet with new projects and the new total amount of hours. I don't have much VBA experience, but I figured this would be the only way to do this. Please let me know if you have any tips on where I should start!
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: Help with making a cumulative timesheet automated

    So you did a great thing posting a sample data set so I could play with it during lunch.

    Attached you will find what you are looking for.
    TimeSheet.xlsb

    I added a ton of comments to the code so you can see what I am doing for each line.
    Option Explicit
    
    Sub ConsolidateMyData()
    
    '===================================================================
    'Declare Variables
    '===================================================================
        Dim DataLastRow As Integer 'Used to find the last row used on the DATA tab
        Dim MonthLastRow As Integer 'Used to find the last row used on the month tabs
        Dim TotalsTab As String 'Used to define which tab to return to at the end
        Dim ws As Worksheet ' Used to loop through the worksheets
        
    '===================================================================
    'Define Variables
    '===================================================================
        TotalsTab = ThisWorkbook.Sheets("Totals").Name 'Defines the tab name
        DataLastRow = ThisWorkbook.Worksheets("Data").Cells(Rows.Count, 1).End(xlUp).Row + 1 ' Finds the FIRST unused row on the DATA tab
    
    '===================================================================
    'Setup For Speed
    '===================================================================
        Application.ScreenUpdating = False 'turns off Screen Updating
        Application.Calculation = xlCalculationManual 'turns off Calculations
        
    '===================================================================
    'Clear Data tab before begining
    '===================================================================
        ThisWorkbook.Sheets("Data").Activate
        Range("A2:E1048576").Clear 'Simply clears the Data tab except for the headers
    
    '===================================================================
    'This copies and pastes through each sheet
    '===================================================================
        For Each ws In Worksheets
            Application.StatusBar = "Aggregating worksheet: " & ws.Name ' just making the
                ws.Activate
            
            If Range("A1").Value = "Employee" And Range("B1").Value = "Project" And ws.Name <> "Data" Then 'this is all to detemine if the the tab houses data , we can change it if need be
                DataLastRow = ThisWorkbook.Worksheets("Data").Cells(Rows.Count, 1).End(xlUp).Row + 1 'used to find first empty row on data tab
                MonthLastRow = ThisWorkbook.Worksheets(ws.Name).Cells(Rows.Count, 1).End(xlUp).Row ' used to find last row on month tab
                    ThisWorkbook.Worksheets(ws.Name).Range("A2:E" & MonthLastRow).Copy ThisWorkbook.Worksheets("Data").Range("A" & DataLastRow) 'copies all of month tab to bottom of DATA tab
            End If
        Next ws
    
    '===================================================================
    'End Macro Procedures
    '===================================================================
        ThisWorkbook.Sheets(TotalsTab).Activate 'Returns to the TotalsTab
        Application.StatusBar = False 'Clears the Statusbar
        Application.ScreenUpdating = True 'turns screen updating back on
        Application.Calculation = xlCalculationAutomatic ' turns calculations back on
        ThisWorkbook.RefreshAll 'Refreshes all pivot tables
        MsgBox "Data Consolidation Complete" 'offers a message box
    
    End Sub


    A few notes:
    1) You really don’t NEED VBA to create the aggregated data, you could just copy and paste the 12 tabs into ONE data tab
    2) At this point the Pivot Table will do everything else, and if you don’t know pivot tables you NEED to learn them as they are SLIIICK and probably one of the most powerful tools in EXCEL
    3) Also they are VERY easy to use, once you have standardized data like you have
    4) Then I took this one step further and made a dynamic named range for you called PivotDATA
    4a)PivotData counts the number of rows used in column A then counts the numbers of columns used in Row 1, and adjusts to perfectly match that, so as you increase or decrease in size it will automatically adjust
    5)SO now that you have the VBA to copy and paste, the formula to dynamically adjust the pivot range, AND the pivot table you never need to do anything but enter data.
    6)Impress Boss and Prosper.


    If you have any questions just ask.
    Please ensure you mark your thread as Solved once it is. Click here to see how.
    If a post helps, please don't forget to add to our reputation by clicking the star icon in the bottom left-hand corner of a post.

  3. #3
    Registered User
    Join Date
    07-21-2014
    Location
    San Diego
    MS-Off Ver
    Office 2010
    Posts
    8

    Re: Help with making a cumulative timesheet automated

    Mike. You are the man! I don't know how to thank you for this. It's EXACTLY what I'm looking for. Now i'm going to go through it with a fine toothed comb to understand every step you take. Thanks for your help!

  4. #4
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: Help with making a cumulative timesheet automated

    Cool, I find doing stuff like this helps me learn new tricks that make me better at what I do at work, especially because I have only been playing with VBA off and on for a year or so.

    I commented the code pretty well but let me know if you have any questions and/or just start another thread.

  5. #5
    Registered User
    Join Date
    07-21-2014
    Location
    San Diego
    MS-Off Ver
    Office 2010
    Posts
    8

    Re: Help with making a cumulative timesheet automated

    Yeah it's working perfectly for me. I had to modify some because due to the formatting of my actual spreadsheet, but it's exactly what I want. However, I have columns for hours, one for hours and one for overtime. In my DATA tab I want a last column to take the summation of both of these columns. I figure this will be an easy command, but where and how do I add it?

  6. #6
    Registered User
    Join Date
    07-21-2014
    Location
    San Diego
    MS-Off Ver
    Office 2010
    Posts
    8

    Re: Help with making a cumulative timesheet automated

    Hey Mike I've changed some more things and for some reason it's looping through all of the worksheets twice and adding them to DATA twice. Can you tell me why it's doing that? Here's the code so far:

    Option Explicit
    
    Sub ConsolidateMyData()
    
    '===================================================================
    'Declare Variables
    '===================================================================
        Dim DataLastRow As Integer 'Used to find the last row used on the DATA tab
        Dim MonthLastRow As Integer 'Used to find the last row used on the month tabs
        Dim TotalsTab As String 'Used to define which tab to return to at the end
        Dim ws As Worksheet ' Used to loop through the worksheets
        
    '===================================================================
    'Define Variables
    '===================================================================
        TotalsTab = ThisWorkbook.Sheets("Totals").Name 'Defines the tab name
        DataLastRow = ThisWorkbook.Worksheets("Data").Cells(Rows.Count, 1).End(xlUp).Row + 1 ' Finds the FIRST unused row on the DATA tab
    
    '===================================================================
    'Setup For Speed
    '===================================================================
        Application.ScreenUpdating = False 'turns off Screen Updating
        Application.Calculation = xlCalculationManual 'turns off Calculations
        
    '===================================================================
    'Clear Data tab before begining
    '===================================================================
        ThisWorkbook.Sheets("Data").Activate
        Range("A2:I1048576").Clear 'Simply clears the Data tab except for the headers
    
    '===================================================================
    'This isnt Necessary, only to use the statusbar to indicate how far along you are
    '===================================================================
        For Each ws In Worksheets
            Application.StatusBar = "Aggregating worksheet: " & ws.Name ' just making the
                ws.Activate
            
            If Len(Range("A1").Value) > 1 And ws.Name <> "Data" Then 'this is all to detemine if the the tab houses data , we can change it if need be
                DataLastRow = ThisWorkbook.Worksheets("Data").Cells(Rows.Count, 1).End(xlUp).Row + 1 'used to find first empty row on data tab
                MonthLastRow = ThisWorkbook.Worksheets(ws.Name).Cells(Rows.Count, 1).End(xlUp).Row ' used to find last row on month tab
                    ThisWorkbook.Worksheets(ws.Name).Range("A2:I" & MonthLastRow).Copy ThisWorkbook.Worksheets("Data").Range("A" & DataLastRow) 'copies all of month tab to bottom of DATA tab
            End If
        Next ws
    
    '===================================================================
    'End Macro Procedures
    '===================================================================
        ThisWorkbook.Sheets(TotalsTab).Activate 'Returns to the TotalsTab
        Application.StatusBar = False 'Clears the Statusbar
        Application.ScreenUpdating = True 'turns screen updating back on
        Application.Calculation = xlCalculationAutomatic ' turns calculations back on
        ThisWorkbook.RefreshAll 'Refreshes all pivot tables
        MsgBox "Complete" 'offers a message box
    
    End Sub

  7. #7
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: Help with making a cumulative timesheet automated

    So the code looks fine, so I am thinking it is the naming convention of your DATA tab.

    VBA is very odd in that it thinks "DATA" is NOT the same as "Data". I had it all uppercase, and you changed the code to be NOT uppercase so the If statement was NOT skipping the DATA tab, so it copied and pasted itself. Lesson learned for you

    Option Explicit
    
    Sub ConsolidateMyData()
    
    '===================================================================
    'Declare Variables
    '===================================================================
        Dim DataLastRow As Integer 'Used to find the last row used on the DATA tab
        Dim MonthLastRow As Integer 'Used to find the last row used on the month tabs
        Dim TotalsTab As String 'Used to define which tab to return to at the end
        Dim ws As Worksheet ' Used to loop through the worksheets
        
    '===================================================================
    'Define Variables
    '===================================================================
        TotalsTab = ThisWorkbook.Sheets("Totals").Name 'Defines the tab name
        DataLastRow = ThisWorkbook.Worksheets("Data").Cells(Rows.Count, 1).End(xlUp).Row + 1 ' Finds the FIRST unused row on the DATA tab
    
    '===================================================================
    'Setup For Speed
    '===================================================================
        Application.ScreenUpdating = False 'turns off Screen Updating
        Application.Calculation = xlCalculationManual 'turns off Calculations
        
    '===================================================================
    'Clear Data tab before begining
    '===================================================================
        ThisWorkbook.Sheets("Data").Activate
        Range("A2:I1048576").Clear 'Simply clears the Data tab except for the headers
    
    '===================================================================
    'Loop through tabs and Copy and Paste Data
    '===================================================================
        For Each ws In Worksheets
            Application.StatusBar = "Aggregating worksheet: " & ws.Name ' just making the
                ws.Activate
            
            If Len(Range("A1").Value) > 1 And UCase(ws.Name) <> "DATA" Then 'this is all to detemine if the the tab houses data , we can change it if need be
                DataLastRow = ThisWorkbook.Worksheets("Data").Cells(Rows.Count, 1).End(xlUp).Row + 1 'used to find first empty row on data tab
                MonthLastRow = ThisWorkbook.Worksheets(ws.Name).Cells(Rows.Count, 1).End(xlUp).Row ' used to find last row on month tab
                    ThisWorkbook.Worksheets(ws.Name).Range("A2:I" & MonthLastRow).Copy ThisWorkbook.Worksheets("Data").Range("A" & DataLastRow) 'copies all of month tab to bottom of DATA tab
            End If
        Next ws
    
    '===================================================================
    'End Macro Procedures
    '===================================================================
        ThisWorkbook.Sheets(TotalsTab).Activate 'Returns to the TotalsTab
        Application.StatusBar = False 'Clears the Statusbar
        Application.ScreenUpdating = True 'turns screen updating back on
        Application.Calculation = xlCalculationAutomatic ' turns calculations back on
        ThisWorkbook.RefreshAll 'Refreshes all pivot tables
        MsgBox "Complete" 'offers a message box
    
    End Sub
    Also, do you know how to walk through the code line by line with the auditing tools? Print this and read through it, it is VERY helpful.
    http://www.cpearson.com/excel/DebuggingVBA.aspx
    Last edited by mikeTRON; 07-21-2014 at 06:49 PM.

  8. #8
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: Help with making a cumulative timesheet automated

    Alternatively you could do something like this:

    If ws.Name = "Jan" Or ws.Name = "Feb" Or _
       ws.Name = "Mar" Or ws.Name = "Apr" Or _
       ws.Name = "May" Or ws.Name = "Jun" Or _
       ws.Name = "Jul" Or ws.Name = "Aug" Or _
       ws.Name = "Sep" Or ws.Name = "Oct" Or _
       ws.Name = "Nov" Or ws.Name = "Dec" Then
    It really just depends what is the cleanest or easiest way to logically break down what you are trying to accomplish

    If you wanted you COULD move the DATA down one row, and adjust the macro accordingly so DATA is also blank on A1... Plenty of ways to make it more idiot proof :P And then when you find a better idiot, you just add a little more code to prevent if from happening again...
    Last edited by mikeTRON; 07-21-2014 at 06:59 PM.

  9. #9
    Registered User
    Join Date
    07-21-2014
    Location
    San Diego
    MS-Off Ver
    Office 2010
    Posts
    8

    Re: Help with making a cumulative timesheet automated

    Thanks again for your help. I was wondering that at the very beginning because you actually had it as "Data" too. I guess I just didn't notice it until I put my actual information into the spreadsheet. I showed my boss and he loved it, so thanks for your help!

    Except now he wants a separate tab that displays only one month at a time. So I want a separate "Month" tab that will ask which month you want, then spit out a PivotTable for that month only. Could you point me in the right direction as to how to do that?

  10. #10
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: Help with making a cumulative timesheet automated

    right click on the Totals tab,
    Move or Copy
    check on Create A Copy
    select where you want the copied tab to go
    click ok
    rename the tab

    Now for modifying the Pivot Table to play with time periods.

    Click in the pivot table and if the field list is not visible right click and click on Show field list near the bottom of the popup

    Now grab the DATE field and drop it into the Row Label before Project.
    Then go right-click in any of the dates in the pivot table itself.
    Click on Group, and because this is a Date you can do all kinds of neat groupings (Month, Quarters, Years)
    Choose Month and click Okay

    Now you can either leave the Date in the rows OR you can drag Date from Row Label to Report Filter.

  11. #11
    Registered User
    Join Date
    07-21-2014
    Location
    San Diego
    MS-Off Ver
    Office 2010
    Posts
    8

    Re: Help with making a cumulative timesheet automated

    I can't even thank you enough. I found a different way to do this last night but I like your idea much better. I have one last question though, then I'll be completely finished with this workbook. On my actual spreadsheet I have a column for regular hours, overtime hours, and double overtime hours. I also want a column that adds all of these up for me. What's the easiest way to do that? Right now I have a formula in my DATA tab, but that's not the most robust way to go about this I'm sure.

  12. #12
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: Help with making a cumulative timesheet automated

    Well I would probably add the hours on the DATA tab like you did, then you can have any or all of those fields for use in the pivot table.

    You COULD do create a calcualtion in the pivot table, and it works fine but it is not quiet as easy to audit (as you dont immediately know where the data is coming from) where your method allows you to easily trace the math and such if you have any questions.

    Attach your most recent file and I can take a look at what specifically you are trying to do.

  13. #13
    Registered User
    Join Date
    07-21-2014
    Location
    San Diego
    MS-Off Ver
    Office 2010
    Posts
    8

    Re: Help with making a cumulative timesheet automated

    Here's what I did. It just adds columns G, H, and I together into column J to get total hours. I'm sure there's a way to put it so that I don't have to write columns G, H, and I separately, but it works for me. And that's all I need, thanks for your help, I really couldn't have done it without you. Well in two days at least.

    For j = 2 To lastrowdata
        ThisWorkbook.Sheets("DATA").Range("J" & j).Value = ThisWorkbook.Sheets("DATA").Range("G" & j) + ThisWorkbook.Sheets("DATA").Range("H" & j) + ThisWorkbook.Sheets("DATA").Range("I" & j)
    Next j

  14. #14
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: Help with making a cumulative timesheet automated

    Can you attach the workbook? It is easier for me to see it in the book than read about it on the forum.

  15. #15
    Registered User
    Join Date
    07-21-2014
    Location
    San Diego
    MS-Off Ver
    Office 2010
    Posts
    8

    Re: Help with making a cumulative timesheet automated

    The code might be pretty confusing, but I have a good gameplan to do what I'm doing. But I need to search through the j'th column for a string and if it isn't there then I'm moving to the next column to search for it. This is in case the separate columns are out of order then the program will still run in the desired way. I've looked into putting Columns(j) inside of the Range() but it never seems to work for some reason.

    EDIT: I attached my worksheet so far. Let me know what you think I need to change or if you have any questions. Check TimeSheet_Dummy, ignore the others. I couldn't figure out how to delete the attachments.
    Attached Files Attached Files
    Last edited by gevans246; 07-22-2014 at 08:18 PM.

+ 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. Automated Timesheet Help: Message Box & Submit Buttons
    By GSA_Tanya in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 01-21-2014, 10:10 PM
  2. Excel 2007 : Generating Automated Timesheet
    By shiven.k in forum Excel General
    Replies: 0
    Last Post: 05-11-2012, 02:50 AM
  3. [SOLVED] Making a timesheet!!!
    By Pat in forum Excel General
    Replies: 2
    Last Post: 03-15-2006, 01:55 PM
  4. help! making a worksheet more automated?
    By redb in forum Excel General
    Replies: 2
    Last Post: 04-15-2005, 09:06 AM
  5. [SOLVED] making a timesheet
    By bobbyd in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-10-2005, 04:06 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