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
Solved

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

Posted on 2011-09-09
2
292 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 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

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 tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

809 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