[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do I code the creation of a pivot table in vb

Posted on 2011-10-18
10
Medium Priority
?
191 Views
Last Modified: 2012-05-12
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.
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

Open in new window

0
Comment
Question by:appc
  • 5
  • 3
9 Comments
 
LVL 39

Expert Comment

by:nutsch
ID: 36987887
Try


Open in new window

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

Open in new window

0
 

Author Comment

by:appc
ID: 36987928
It says syntax error on this part:

    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        sheets("Sheet1!").cells(1,1,).currentregion, Version:=xlPivotTableVersion12).CreatePivotTable _
        TableDestination:=activesheet.cells(3,1), TableName:=sPivotName, DefaultVersion _
        :=xlPivotTableVersion12
0
 
LVL 39

Expert Comment

by:nutsch
ID: 36987975
as it should, I had left an excess !!

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        sheets("Sheet1").cells(1,1,).currentregion, Version:=xlPivotTableVersion12).CreatePivotTable _
        TableDestination:=activesheet.cells(3,1), TableName:=sPivotName, DefaultVersion _
        :=xlPivotTableVersion12
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 39

Expert Comment

by:nutsch
ID: 36987992
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.
0
 

Author Comment

by:appc
ID: 36988062
This:

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        sheets("Sheet1").cells(1,1,).currentregion, Version:=xlPivotTableVersion12).CreatePivotTable _
        TableDestination:=activesheet.cells(3,1), TableName:=sPivotName, DefaultVersion _
        :=xlPivotTableVersion12

does not change anything.
0
 
LVL 39

Expert Comment

by:nutsch
ID: 36988158
Still error on cells(1,1,)

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

Open in new window


Thomas
0
 

Author Comment

by:appc
ID: 36988201
It stopped and said:

error 400
0
 
LVL 39

Accepted Solution

by:
nutsch earned 1000 total points
ID: 36988237
Which line? It works for me in the attached file.

Thomas
Book5.xlsm
0
 
LVL 50
ID: 37419374
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question