Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-09-09
2
Medium Priority
?
300 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 42

Accepted Solution

by:
dlmille earned 2000 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

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.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

824 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