Solved

Copy Worksheet, rename with today's date and change data range for pivot table

Posted on 2011-09-09
2
294 Views
Last Modified: 2012-06-17
Hi Experts,

I use the attached workbook as my timesheet on a daily basis.  The workbook contains a worksheet for each day I'm in the office.  Everyday I copy the worksheet from the day before and rename it to that day's date in the "mm:dd:yy" format, I then change the data range of the pivot table in the new worksheet to be that of the new worksheet.  Also, the attached workbook includes small code that updates the active pivot table when the data in the worksheet changes.

I would like to automate this entire process and this is why I'm turning to you for help.  I would like to accomplish the following when clicking the "Create New Worksheet" button:

1. Copy Active Worksheet

Copy the active worksheet, or in this case the "09-08-11" worksheet, and paste it to the end, which in my workbook would be after "09-08-11" worksheet, since it only contains one worksheet

2. Rename the newly copied worksheet

This newly copied worksheet should be renamed to today's date, "09-09-11", using the "mm:dd:yy" format.

3. Change Pivot Table Data Range

The pivot table in the newly copied worksheet has its data range tied to the source worksheet "09-08-11", so this data range should be changed to the same range in the "09-09-11" worksheet.
I attempted to accomplish this, but I ran into problems where my code was using the cached data range, specifically pointing to a location in my computer.

Thanks much for your help.

Best,


Sura
Timesheet-09-11.xlsm
0
Comment
Question by:SuraDalbin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 42

Accepted Solution

by:
dlmille earned 500 total points
ID: 36515320
App creates new worksheet, renames to same format as original (MM-DD-YY) - though the Format statement could use (MM:DD:YY), at the end of the workbook, then updates the pivot table range (assumed to be a worksheet range, rather than query or other type of connection - for that, see http://support.microsoft.com/kb/816562) to the newly created worksheet.

You have the option to delete an existing sheet with today's date, or cancel/abort.

Here's the code:
Option Explicit

Sub CreateNewWorksheet_Click()
Dim wkb As Workbook
Dim originalSht As Worksheet
Dim newSht As Worksheet, strNewShtName As String
Dim oldPath As String, newPath As String
Dim xMsg As Long
    
    Set wkb = ThisWorkbook
    strNewShtName = Format(Now, "MM-DD-YY") 'alternatively, could be "MM:DD:YY"
    
    On Error Resume Next
    Set newSht = wkb.Sheets(strNewShtName)
    If Err.Number = 0 Then
        xMsg = MsgBox("Sheet for today already exists, delete it?", vbOKCancel, "Hit Ok to Delete, Cancel to abort")
        If xMsg = vbCancel Then
            Exit Sub
        Else
            wkb.Sheets(strNewShtName).Delete
        End If
    End If
    
    On Error GoTo errHandler
    
    Set originalSht = ActiveSheet
    oldPath = originalSht.Name
    
    originalSht.Copy after:=wkb.Sheets(wkb.Sheets.Count)
        
    Set newSht = ActiveSheet
    
    newSht.Name = strNewShtName
    
    newPath = strNewShtName
    
    Call updatePivots(newSht, oldPath, newPath)
    
    MsgBox "Process Complete!", vbOKOnly
    Exit Sub
    
errHandler:
    MsgBox "Error Processing No: " & Err.Number & " -> " & Err.Description
End Sub
Sub updatePivots(ws As Worksheet, oldPath As String, newPath As String)
'simple routine to traverse each pivottable in the ws worksheet, changing references from the old/original sheet to
'the new sheet.  Assumes no query, other file, or other type of connection - simply a reference in the existing workbook
'for more sophisticated conversions, see: http://support.microsoft.com/kb/816562

Dim pt As PivotTable

    
    For Each pt In ws.PivotTables
        pt.ChangePivotCache PivotCache:=ws.Parent.PivotCaches.Create(SourceType:=xlDatabase, _
            SourceData:="" & Application.Substitute(pt.PivotCache.SourceData, oldPath, newPath) & "", _
            Version:=xlPivotTableVersion12)
    Next pt
End Sub

Open in new window

See attached workbook, click the button and you're good to go.  note, you'll get an error if you click the button for current day, when the new day has already been created...

Enjoy!

Dave  
Timesheet-09-11-r2.xlsm
0
 

Author Closing Comment

by:SuraDalbin
ID: 36515508
Thank you very much Dave, this works like a charm!  Have a great weekend!
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

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 article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

751 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