Stephen has attended:
Excel Advanced course
Pivot table usage within macros
When a pivot table is created in a macro the pivot is created in a new sheet which is automatically named Sheet[next number].
Unfortunately this renders the macro unworkable unless by sheer chance the [next number] when the macro is run is the same as the [next number] when the macro was recorded.
Is there a way of naming the sheets on creation to avoid this issue?
RE: Pivot table usage within macros
Hello Stephen,
You could name the sheet before the Pivot is created. Below is the code, it should be identical to your recorded code. I have only included the first part.
The parts you need to change are:
After the Sheets.add, put the inputbox command to accept a name for new sheet.
Look for the Table Destination property and follow this with the Activesheet command and the cell location.
if your code has the 2 lines below the pivot command, mine here shows Sheets 5 and Cells etc.. just comment these out or delete.
hope that helps
Sheets.Add
ActiveSheet.Name = InputBox("Enter name")
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
Sheets("Mark Moore").Range("A10").CurrentRegion, Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:=ActiveSheet.Range("A1"), TableName:="PivotTable2", DefaultVersion _
:=xlPivotTableVersion14
' Sheets("Sheet5").Select
' Cells(3, 1).Select
I hope this resolves your question. If it has, please mark this question as resolved.
If you require further assistance, please reply to this post. Or perhaps you have another Microsoft Office question?
Have a great day.
Regards,
Mark
Microsoft Office Specialist Trainer