Solved

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

Posted on 2011-09-09
2
287 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
2 Comments
 
LVL 41

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now