• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 303
  • Last Modified:

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

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.


1 Solution
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
        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
    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) & "", _
    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...


SuraDalbinAccountantAuthor Commented:
Thank you very much Dave, this works like a charm!  Have a great weekend!

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now