VBA Pivot Table On New Sheet - excel

I have recorded a macro to create a pivot table and subsequent chart in VBA. It works like a charm, just not at all how I need it to. The problem is that I want to be able to run the code and have it create a table on a sheet that doesn't already exist. Basically I will run this from a button on a menu, and it should create the the table and chart on a new page. Without further adieu:
Sub Macro13()
' Macro13 Macro
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Main Budget Sheet!R2C1:R88C10", Version:=xlPivotTableVersion14). _
CreatePivotTable TableDestination:="Sheet21!R1C1", TableName:= _
"PivotTable12", DefaultVersion:=xlPivotTableVersion14
Sheets("Sheet21").Select
Cells(1, 1).Select
With ActiveSheet.PivotTables("PivotTable12").PivotFields("Category")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable12").AddDataField ActiveSheet.PivotTables( _
"PivotTable12").PivotFields("Labor"), "Sum of Labor", xlSum
ActiveSheet.PivotTables("PivotTable12").AddDataField ActiveSheet.PivotTables( _
"PivotTable12").PivotFields("Material"), "Sum of Material", xlSum
ActiveSheet.Shapes.AddChart.Select
End Sub
If I was putting this into plain English, I would assume that it means... Select Data in Main Budget Sheet then create a pivot table on sheet 21 named pivot table 12... Then Select sheet 21 and select the fields for the pivot table...Finally, add chart from that data...
I think what I need to do is just figure out general names for each of these things (besides source data).
Also, currently it will only run once and only if that sheet already exists, presumably because it can't make a pivot table named the same thing, so if it could repeatedly just create the same table on different sheets I could die happy.
As you can see, I've tried this enough that I have created 21 sheets :/ so the effort is there, but as Einstein definition of insanity states "Insanity is trying the same thing and expecting a different result." Currently, I'm running out of new things to try.

Add a new sheet then use that sheet to add the new pivot too as follow:
Sub AddPivotOnNewSheet()
Dim wsNew As Worksheet
Set wsNew = Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Main Budget Sheet!R2C1:R88C10", Version:=xlPivotTableVersion14). _
CreatePivotTable TableDestination:=wsNew.Name & "!R1C1", TableName:= _
"PivotTableName", DefaultVersion:=xlPivotTableVersion14
With ActiveSheet.PivotTables("PivotTableName").PivotFields("Category")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTableName").AddDataField ActiveSheet.PivotTables( _
"PivotTableName").PivotFields("Labor"), "Sum of Labor", xlSum
ActiveSheet.PivotTables("PivotTableName").AddDataField ActiveSheet.PivotTables( _
"PivotTableName").PivotFields("Material"), "Sum of Material", xlSum
wsNew.Shapes.AddChart.Select
End Sub

Sub pivotnewsheet()
Dim ws As Worksheet
Dim pc As PivotCache
Dim pt As PivotTable
'Creating Pivot cache
On Error Resume Next
Set pc = ActiveWorkbook.PivotCaches.Create(xlDatabase, Cells(5, 1).CurrentRegion) 'select first cells of your data range, in my case its start with Row5 column1 so i put cells(5,1)
'Adding new worksheet
Set ws = Worksheets.Add
ws.Name = "Summary"
'Creating Pivot table
Set pt = ActiveSheet.PivotTables.Add(PivotCache:=pc, Tabledestination:=Range("A3"))
' it will create pivot table on sheet named summary at range A3
pt.Name = "PivotTable"
'Setting Fields
With pt
with .PivotFields("DEPT_NAME")
.Orientation = xlRowField
.Position = 1
End With
'set column field
With .PivotFields("QTY")
.Orientation = xlDataField
.Function = xlSum
.Position = 1
End With
With .PivotFields("TOTAL_RETAIL_VALUE")
.Orientation = xlDataField
.Function = xlAverage
.Position = 2
.NumberFormat = "0.00" 'format for two decimals
End With
With .PivotFields("TOTAL_LANDED_COST")
.Orientation = xlDataField
.Function = xlAverage
.NumberFormat = "0.00"
.Position = 3
End With
'select classic view pivot
With ActiveSheet.PivotTables("PivotTable")
.InGridDropZones = True
.RowAxisLayout xlTabularRow
End With
End With
End Sub

Related

Error code: `unable to get the pivottables property of the worksheet class` when same code works for different pivot table

I am writing a macro which creates several pivot tables. All but 1 table runs fine, but I am getting a runtime error code for the one below: unable to get the pivottables property of the worksheet class
Dim PT As Excel.PivotTable
Set PT = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"GFCID!R1C1:R117200C129", Version:=xlPivotTableVersion14).CreatePivotTable( _
TableDestination:="'source'!R1C1", TableName:="Banking Book Pivot", _
DefaultVersion:=xlPivotTableVersion14)
With PT.PivotFields( _
"IRU")
.Orientation = xlRowField
.Position = 1
End With
' ActiveSheet.PivotTables("Banking Book Pivot").AddDataField ActiveSheet.PivotTables( _
' "Banking Book Pivot").PivotFields("GFCID"), "Count of GFCID", xlCount
' PT.AddDataField ActiveSheet.PivotTables( _
' "Banking Book Pivot").PivotFields("GFCID"), "Count of GFCID", xlCount
' With ActiveSheet.PivotTables("Banking Book Pivot").PivotTables("Banking Book Pivot").PivotFields("GFCID" & Chr(10) & "Count")
' .Orientation = xlDataField
' .Function = xlCount
' .Name = "Count of GFCID"
' End With
I have tried each of the three codes which are commented out, and they all get the same error. The first two codes work with other pivot tables in the macro. The data set for this pivot table is much larger than the others, but I don't think that should make a difference.
Don't use ActiveSheet. Use PT:
With PT
.AddDataField .PivotFields("GFCID"), "Count of GFCID", xlCount
End With

Create Pivot Table not working on new Excel version

My macro is supposed to create a Pivot Table and add specific filters.
I used the macro recorder for this, it works on my computer but not on newer versions of excel. Heres my code:
Cells.Select
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"1!R1C1:R1048576C13", Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="Tabelle2!R3C1", TableName:="PivotTable2", _
DefaultVersion:=xlPivotTableVersion14
Sheets("Tabelle2").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable2").PivotFields("PlatzID/Gerät")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
"PivotTable2").PivotFields("Tagabfluss"), "Anzahl von Tagabfluss", xlCount
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Anzahl von Tagabfluss" _
)
.Caption = "Summe von Tagabfluss"
.Function = xlSum
End With
I tried to change Version:=xlPivotTableVersion14 to Version:=xlPivotTableVersion15 and other numbers which are for other excel versions but none seem to work.
I am using Excel 2010 my coworkers are using Excel 2013.
The Debug function highlights:
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"1!R1C1:R1048576C13", Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="Tabelle2!R3C1", TableName:="PivotTable2", _
DefaultVersion:=xlPivotTableVersion14
First off, you really shouldn't use entire columns for the source data for a pivot table. Second, breaking the code up into discrete sections will help you to narrow down whether the error is in creating the pivotcache, or the pivot table itself. I'd suggest using:
Dim pivotSheet As Worksheet
Set pivotSheet = Sheets.Add
With Sheets("1")
Dim pc As PivotCache
Set pc = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
SourceData:="'" & .Name & "'!" & .Range("A1").CurrentRegion.Address(ReferenceStyle:=xlR1C1), _
Version:=xlPivotTableVersion14)
End With
Dim pt As PivotTable
Set pt = pc.CreatePivotTable(TableDestination:=pivotSheet.Cells(3, 1), DefaultVersion:=xlPivotTableVersion14)
With pt
With .PivotFields("PlatzID/Gerät")
.Orientation = xlRowField
.Position = 1
End With
.AddDataField .PivotFields("Tagabfluss"), "Summe von Tagabfluss", xlSum
End With

Excel VBA Macro to create PIVOT Table

I want to create a VBA Macro code to create a Pivot table showing in the image below.
In the report filter I have Aircraft type and in Column Level I have Area and summation value I have Count of Aircraft type.
I record a macro after creating this table but after run the macro I was not be able to create the Pivot showing in my image. I am new here so I am unable to findout how to resolve this issue?
My code is given below:
Sub Macro2()
'
' Macro2 Macro
'
'
Range("A1:B16").Select
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Sheet13!R1C1:R16C2", Version:=xlPivotTableVersion12).CreatePivotTable _
TableDestination:="Sheet14!R3C1", TableName:="PivotTable4", DefaultVersion _
:=xlPivotTableVersion12
Sheets("Sheet14").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable4").PivotFields("Aircraft Type")
.Orientation = xlPageField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable4").PivotFields("Area")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable4").AddDataField ActiveSheet.PivotTables( _
"PivotTable4").PivotFields("Aircraft Type"), "Count of Aircraft Type", xlCount
End Sub

Apply one macro to another sheet/workbook regardless of sheet or workbook name

I have an issue with macros returning errors, and I was wondering if you guys could shed some light on this issue.
The macro I have looks like the following:
Sub pivot1_macro()
'
' pivot1_macro Macro
'
' Keyboard Shortcut: Ctrl+Shift+M
'
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"7-14 week!R1C2:R199C35", Version:=xlPivotTableVersion15).CreatePivotTable _
TableDestination:="Sheet5!R3C1", TableName:="PivotTable3", DefaultVersion _
:=xlPivotTableVersion15
Sheets("Sheet5").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable3").PivotFields("PartMOD")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("EnteredShift")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Defect")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
"PivotTable3").PivotFields("Defect Quantity"), "Sum of Defect Quantity", xlSum
Columns("D:D").EntireColumn.AutoFit
Columns("A:A").EntireColumn.AutoFit
Columns("B:B").EntireColumn.AutoFit
Columns("C:C").EntireColumn.AutoFit
Columns("D:D").EntireColumn.AutoFit
Range("B3").Select
Range("B3").Select
End Sub
I'd like to apply this macro to any active worksheet, but I'm not quite sure
how to make set 'SourceData' to current active worksheet.
You can use '&' to concatenate strings, so wouldn't
SourceData := ActiveSheet.Name & "!R1C2:R199C35"
work for you to get the current sheet's name?

Create Pivot Table

I got this Run-time error '1004':- This command requires at least two rows of source data. You cannot use the command on a selection in only one row. Try the following: If you're using an advanced filter, select a range of cells that contains at least two rows of data. Then click the Advanced Filter command again. If you're creating a PivotTable report or PivotChart report, type a [it stops here]
I got the aforementioned error at:-
pvc.CreatePivotTable TableDestination:=Worksheets("TABLE").Range("A1"), _
TableName:="INFO", DefaultVersion:=xlPivotTableVersion12
I'm trying to run this code:-
Sub CreateTable()
Dim lastRow As Long
Dim pvc As PivotCache
lastRow = ActiveSheet.Cells(Rows.Count, 6).End(xlUp).Row
ActiveWorkbook.Names.Add Name:="Database", RefersTo:="=DATA!$G$1:$K$" & lastRow
Set pvc = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Database", Version:=xlPivotTableVersion12)
pvc.CreatePivotTable TableDestination:=Worksheets("TABLE").Range("A1"), _
TableName:="INFO", DefaultVersion:=xlPivotTableVersion12
Sheets("TABLE").Select
Cells(1, 1).Select
ActiveWorkbook.ShowPivotTableFieldList = True
With ActiveSheet.PivotTables("INFO").PivotFields("MODEL")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("INFO").PivotFields("TYPE")
.Orientation = xlRowField
.Position = 2
End With
ActiveSheet.PivotTables("INFO").AddDataField ActiveSheet.PivotTables( _
"INFO").PivotFields("GRADE"), "Sum of GRADE", xlSum
ActiveSheet.PivotTables("INFO").AddDataField ActiveSheet.PivotTables( _
"INFO").PivotFields("SIZE"), "Sum of SIZE", xlSum
ActiveSheet.PivotTables("INFO").AddDataField ActiveSheet.PivotTables( _
"INFO").PivotFields("QTY"), "Sum of QTY", xlSum
With ActiveSheet.PivotTables("INFO").PivotFields("MODEL")
.Orientation = xlColumnField
.Position = 2
End With
With ActiveSheet.PivotTables("INFO").PivotFields("TYPE")
.Orientation = xlColumnField
.Position = 3
End With
With ActiveSheet.PivotTables("INFO").PivotFields("Sum of GRADE")
.Orientation = xlPINFOField
.Position = 1
End With
With ActiveSheet.PivotTables("INFO").PivotFields("Sum of SIZE")
.Orientation = xlRowField
.Position = 1
End With
ActiveWorkbook.ShowPivotTableFieldList = False
Range("B3").Select
ActiveSheet.PivotTables("INFO").CompactLayoutColumnHeader = "MODEL"
Range("A5").Select
ActiveSheet.PivotTables("INFO").CompactLayoutRowHeader = "SIZE"
Range("A1").Select
ActiveSheet.PivotTables("INFO").PivotFields("GRADE").Caption = "GRADE"
Cells.Select
Cells.EntireColumn.AutoFit
Columns("B:BB").Select
Selection.Style = "Comma"
Cells.Select
Cells.EntireColumn.AutoFit
Range("C1").Select
End Sub
Pls help.
Thanks.
It appears the reason that this error occurs is due to the fourth row:
lastRow = ActiveSheet.Cells(Rows.Count, 6).End(xlUp).Row
by the look of this, you are finding the last row of data in your "DATA" sheet by looking in column 6. Now if we look what you are combining this with:
ActiveWorkbook.Names.Add Name:="Database", RefersTo:="=DATA!$G$1:$K$" & lastRow
Now, if your source data is in cells G1:K** then:
ActiveSheet.Cells(Rows.Count, 6).End(xlUp).Row
will always return 1 unless you have data in column 6, but from the line above, it seems that your data starts in column 7 ("G"). Therefore, to get this to work you have to change the fourth row to:
lastRow = ActiveSheet.Cells(Rows.Count, 7).End(xlUp).Row

Resources