Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default pivottable name in a macro

I have recorded a macro to create a pivottable. The macro includes a
pivottable name which is specific to the table e.g. TableName:="PivotTable1"
as below

ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatab ase, SourceData:= _
"Sheet1!R25C10:R30C12").CreatePivotTable TableDestination:= _
"[Book2]Sheet1!R36C10", TableName:="PivotTable1", DefaultVersion:= _
xlPivotTableVersion10
ActiveSheet.PivotTables("PivotTable1").AddFields RowFields:="a"
ActiveSheet.PivotTables("PivotTable1").PivotFields ("c").Orientation = _
xlDataField
ActiveWorkbook.ShowPivotTableFieldList = False
Application.CommandBars("PivotTable").Visible = False

Can anybody tell me how to make this macro allocate the next pivottable
number to use in the spreadsheet rather than re-use "PivotTable1" each time
which seems to cause an error when the table already exists ?

Thanks

Nigel
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default pivottable name in a macro


try something like this:

Dim wrksht As Worksheet
Dim pivtble As PivotTable
Dim pivotCounter As Integer

pivotCounter = 0

For Each wrksht In ActiveWorkbook.Worksheets

For Each pivtble In wrksht.PivotTables

If Right(pivtble.Name, 1) pivotCounter Then
pivotCounter = Right(pivtble.Name, 1)
End If

Next pivtble

Next wrksht

pivotCounter = pivotCounter + 1


ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatab ase, _
SourceData:="Sheet1!R25C10:R30C12").CreatePivotTab le TableDestination:=
_
"[Book2]Sheet1!R36C10", TableName:="PivotTable" & pivotCounter,
DefaultVersion:= _
xlPivotTableVersion10
ActiveSheet.PivotTables("PivotTable" & pivotCounter).AddFields
RowFields:="a"
ActiveSheet.PivotTables("PivotTable" &
pivotCounter).PivotFields("c").Orientation = _
xlDataField

Pete


--
Peter81
------------------------------------------------------------------------
Peter81's Profile: http://www.excelforum.com/member.php...o&userid=25353
View this thread: http://www.excelforum.com/showthread...hreadid=531446

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default pivottable name in a macro

It works for me - Thanks for your help

"Peter81" wrote:


try something like this:

Dim wrksht As Worksheet
Dim pivtble As PivotTable
Dim pivotCounter As Integer

pivotCounter = 0

For Each wrksht In ActiveWorkbook.Worksheets

For Each pivtble In wrksht.PivotTables

If Right(pivtble.Name, 1) pivotCounter Then
pivotCounter = Right(pivtble.Name, 1)
End If

Next pivtble

Next wrksht

pivotCounter = pivotCounter + 1


ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatab ase, _
SourceData:="Sheet1!R25C10:R30C12").CreatePivotTab le TableDestination:=
_
"[Book2]Sheet1!R36C10", TableName:="PivotTable" & pivotCounter,
DefaultVersion:= _
xlPivotTableVersion10
ActiveSheet.PivotTables("PivotTable" & pivotCounter).AddFields
RowFields:="a"
ActiveSheet.PivotTables("PivotTable" &
pivotCounter).PivotFields("c").Orientation = _
xlDataField

Pete


--
Peter81
------------------------------------------------------------------------
Peter81's Profile: http://www.excelforum.com/member.php...o&userid=25353
View this thread: http://www.excelforum.com/showthread...hreadid=531446


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
2007 / 2003 Pivottable Macro jxbeeman Excel Discussion (Misc queries) 0 July 21st 09 08:51 PM
Create a Pivottable in a macro Michael Excel Discussion (Misc queries) 8 March 15th 07 02:41 PM
excel2000 pivottable macro רוזנט Excel Programming 4 June 30th 05 05:02 PM
PivotTable, Scenarios, Macro?? Dave Wilson New Users to Excel 4 December 18th 04 12:22 PM
Creating a database and PivotTable with a Macro sales26 Excel Programming 2 May 19th 04 05:05 PM


All times are GMT +1. The time now is 07:38 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"