appc
asked on
How do I code the creation of a pivot table in vb
I tried using the macro recorder to understand how I can code a pivot table creation in vb but it doesn't work when I try reusing the macro.
I says "Invalid procedure, call or argument".
Please let me know what is wrong.
I says "Invalid procedure, call or argument".
Please let me know what is wrong.
Sub Macro1()
'
' Macro1 Macro
'
'
Application.CutCopyMode = False
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Sheet1!R1C1:R4990C46", Version:=xlPivotTableVersion12).CreatePivotTable _
TableDestination:="Sheet4!R3C1", TableName:="PivotTable58", DefaultVersion _
:=xlPivotTableVersion12
Sheets("Sheet4").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable58").PivotFields("Original_Portfolio")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable58").AddDataField ActiveSheet.PivotTables( _
"PivotTable58").PivotFields("NPV"), "Sum of NPV", xlSum
End Sub
ASKER
It says syntax error on this part:
ActiveWorkbook.PivotCaches .Create(So urceType:= xlDatabase , SourceData:= _
sheets("Sheet1!").cells(1, 1,).curren tregion, Version:=xlPivotTableVersi on12).Crea tePivotTab le _
TableDestination:=activesh eet.cells( 3,1), TableName:=sPivotName, DefaultVersion _
:=xlPivotTableVersion12
ActiveWorkbook.PivotCaches
sheets("Sheet1!").cells(1,
TableDestination:=activesh
:=xlPivotTableVersion12
as it should, I had left an excess !!
ActiveWorkbook.PivotCaches .Create(So urceType:= xlDatabase , SourceData:= _
sheets("Sheet1").cells(1,1 ,).current region, Version:=xlPivotTableVersi on12).Crea tePivotTab le _
TableDestination:=activesh eet.cells( 3,1), TableName:=sPivotName, DefaultVersion _
:=xlPivotTableVersion12
ActiveWorkbook.PivotCaches
sheets("Sheet1").cells(1,1
TableDestination:=activesh
:=xlPivotTableVersion12
Your initial code was trying to recreate Pivottable58 on sheet4 (which causes issues).
I'm trying to replace the hardcoded destinations with more flexible ones so your code works every time.
I'm trying to replace the hardcoded destinations with more flexible ones so your code works every time.
ASKER
This:
ActiveWorkbook.PivotCaches .Create(So urceType:= xlDatabase , SourceData:= _
sheets("Sheet1").cells(1,1 ,).current region, Version:=xlPivotTableVersi on12).Crea tePivotTab le _
TableDestination:=activesh eet.cells( 3,1), TableName:=sPivotName, DefaultVersion _
:=xlPivotTableVersion12
does not change anything.
ActiveWorkbook.PivotCaches
sheets("Sheet1").cells(1,1
TableDestination:=activesh
:=xlPivotTableVersion12
does not change anything.
Still error on cells(1,1,)
Here is a tested version:
Thomas
Here is a tested version:
Sub Macro1()
'
' Macro1 Macro
'
Dim sPivotName As String, pt As PivotTable
sPivotName = "Pivot_" & Format(Now(), "ddmm_hhmmss")
'
Application.CutCopyMode = False
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
Sheets("Sheet1").Cells(1, 1).CurrentRegion, Version:=xlPivotTableVersion12).CreatePivotTable _
TableDestination:=ActiveSheet.Cells(3, 1), TableName:=sPivotName, DefaultVersion _
:=xlPivotTableVersion12
Set pt = ActiveSheet.PivotTables(sPivotName)
With pt.PivotFields("Original_Portfolio")
.Orientation = xlColumnField
.Position = 1
End With
pt.AddDataField pt.PivotFields("NPV"), "Sum of NPV", xlSum
End Sub
Thomas
ASKER
It stopped and said:
error 400
error 400
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
Open in new window
Open in new window