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
Related
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
I basically have some data and I need to count how many times each row (titled "Process") occurs (using VBA). I used the built-in Excel macro, and used a pivot table to count the number of instances of each row, however, the problem is that when I run the macro, it only returns the total count of process.
This is my code:
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
DataSheet = ActiveSheet.Name
Sheets.Add
newsheet = ActiveSheet.Name
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
DataSheet & "!R1C1:R" & FinalRow & "C52", Version:=6).CreatePivotTable TableDestination:= _
newsheet & "!R3C1", TableName:="PivotTable24", DefaultVersion:=6
Sheets(newsheet).Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable24").PivotFields("Process")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable24").AddDataField ActiveSheet.PivotTables( _
"PivotTable24").PivotFields("Process"), "Count of Process", xlCount
End Sub
If I remove the code between End With and End Sub, I get a pivot table with the rows as the list of processes. But when I add those two lines, I just get a value titled "Count of Process" with the Grand Total value. I am pretty sure that the issue lies somewhere there. Thanks.
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?
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
I'm trying to put a macro together that will make a simple pivot table using the data from an active worksheet. When I try to run it, I receive a type mismatch error. When I start the debugger, the first section is highlighted: ActiveWorkbook.PivotCaches through xlPivotTableVersion10. Initially, the TableDestination was blank and I thought that might be the problem, but after adding a destination I still get the same error.
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
SourceData:=ActiveSheet.UsedRange).CreatePivotTable TableDestination:="Sheet1!R3C1", _
TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion10
ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(1, 1)
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Program Name")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Dollars Awarded"), "Sum of Dollars Awarded", xlSum
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Dept Head")
.Orientation = xlRowField
.Position = 1
End With
This worked for me (XL2007):
Sub Tester()
With ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
SourceData:=ActiveSheet.UsedRange)
.CreatePivotTable TableDestination:="Sheet1!R3C1", _
TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion10
End With
With Sheet1.PivotTables("PivotTable1")
.PivotFields("Dept Head").Orientation = xlColumnField
.PivotFields("Program Name").Orientation = xlRowField
.AddDataField .PivotFields("Cost"), "Sum of cost", xlSum
End With
End Sub
Make sure you don't already have an existing conflicting pivot cache/table.