Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

pivot table usage within

Forum home » Delegate support and help forum » Microsoft Excel Training and help » Pivot table usage within macros

Pivot table usage within macros

ResolvedVersion 2007

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

Excel tip:

Repeating headings on spreadsheets that print on more than one page

By default when you print a spreadsheet out and it prints on more than one page, the headings at the top and the side of the spreadsheet don't appear on all the pages following page 1.

To get Excel to repeat headings on all pages when printing, go to File - Page Setup - Sheet, then select the rows to repeat at the top of pages, and the columns to repeat at the side of pages by clicking on the red arrows at the right side of the two boxes under the 'Print titles' area. Then click OK.

If you view your spreadsheet in Print Preview, you should see the headings being repeated on each page.

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.14 secs.