Rename a worksheet to use with a pivot table

D

David J

I am consolidating date every two weeks into this workbook and each time I
will change the worksheet name to match the current period i.e. "Period X".
I have the pivot table on another worksheet and want to automatically change
the source data worksheet name to match the current period or allow a user to
input the current period number. Here is the code I have for doing this, but
I have a syntax error I cannot figure out.
PeriodNumber = InputBox(Prompt:="Enter the Period Number for the Summary
Report", Title:="Summary Report Period")
WSName = "Period " & PeriodNumber

'The error is in this portion of the code

ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=
WorkSheet("WSName").Range("C10:C14").CreatePivotTable(TableDestination:="'Period Report'!R3C1", TableName:="PivotTable2")

ActiveSheet.PivotTables("PivotTable2").RowGrand = False
ActiveSheet.PivotTables("PivotTable2").AddFields RowFields:=Array( _
"Billing Code", "Client Name", "Project No."), ColumnFields:="Name"
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Total")
.Orientation = xlDataField
.Caption = "Sum of Total"
.Function = xlSum
End With
End Sub
 
D

Dave Peterson

Try...

...., sourcedata:=WorkSheet(WSName).range(....

(drop the double quotes)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top