Hi everyone. I've got a macro that I've been running pretty flawlessly each week for an hours tracking workbook I use.
Basically, I have a Table that contains information I enter daily regarding my projects and the hours associated with them. The table is summarized in a Pivot Table as well, for end-of-the-week reporting in an external application. My macro just copies this sheet, table, and pivot table, renames things for the new week, and lets me repeat the hours accounting info each week.
This macro (code below) has been working well for quite some time, but in the last month or two (I don't remember when) it started throwing up warnings about a list name already existing, and I can either click Yes to use that version of the name, or click No to rename it. This comes up for 4 names of previous lists, all in chronological order (but skipping some lists from previous sheets). It's obvious to me from debugging that it happens when I use the Sheets(1).Copy Before:=Sheets(1) command. What's not obvious is why this should give me 4 list name collisions instead of just one, and why it's picking these 4 specific lists out of many that are in the workbook.
I confess that I made most of this code by just manually doing the steps and recording the macro as I went, so it's entirely possible that I'm doing a million different things very poorly from a programming standpoint. Please don't laugh too badly at my code 
Option Explicit
Sub NewWeekSheet()
Dim strMonth As String, strDay As String, strYear As String, strDate As String
Dim iMondayOffset As Integer
Dim iLastRow As Integer
strMonth = CStr(Month(Now()))
iMondayOffset = Weekday(Now()) - vbMonday
strDay = CStr(Day(Now() - iMondayOffset))
strYear = Right(CStr(Year(Now())), 2)
strDate = strMonth & "-" & strDay & "-" & strYear
Application.ScreenUpdating = False
Sheets(1).Copy Before:=Sheets(1)
Sheets(1).Name = strDate
iLastRow = Range("A1").End(xlDown).Row
Range("A4:H" & iLastRow).ClearContents
ActiveSheet.ListObjects(1).Resize Range("$A$1:$H$3")
strDate = "_" & Replace(strDate, "-", "_") 'Do this to make the name format happy in Excel
ActiveSheet.ListObjects(1).Name = strDate & "_List"
Range("B2:D3,F2:H3").ClearContents
Range("B2").Value = Format(Now() - iMondayOffset, "mm/dd/yy")
ActiveSheet.PivotTables(1).ChangePivotCache ActiveWorkbook. _
PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
strDate & "_List[[#Headers],[#Data]]", Version:=xlPivotTableVersion12)
ActiveSheet.PivotTables(1).Name = strDate & "_Table"
ActiveSheet.PivotTables(1).PivotCache.Refresh
ActiveWorkbook.ShowPivotTableFieldList = False
Range("C2").Select
Application.ScreenUpdating = True
End Sub
Can anyone look at what I did, and more importantly, what I did WRONG? And, provide me some guidance on how I can fix it?
Thanks in advance for any helpful insights.
-Adam
Bookmarks