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.
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
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
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
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 be able to use this macro to create this pivot table each day on the same worksheet name. The only thing that will change is the datasource row as I never know how many rows it will be. I tried to set the entire string into the CurSourceData variable and then just use that but it fails and says invalid arg.
Any ideas would be helpful.
Sheets("Cases 23+ Day (Due Today)").Select
Range("A1").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
CurRow = ActiveCell.Row
CurSourceData = "Cases 23+ Day (Due today)!R1C2:R" & CurRow & "C20"
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
CurSourceData, Version:=xlPivotTableVersion14). _
CreatePivotTable TableDestination:="Master Summary!R3C7", TableName:= _
"PivotTable3", DefaultVersion:=xlPivotTableVersion14
Sheets("Master Summary").Select
Cells(3, 7).Select
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
"PivotTable3").PivotFields("Status"), "Count of Status", xlCount
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Status")
.Orientation = xlRowField
.Position = 1
End With
The SourceData argument of PivotCaches.Create is either a Range or a Connection. I think there's also a problem with the space in your sheet name and the answer is to use single quotes. Try :
Set CurSourceData = Worksheets("Cases 23+ Day (Due today)").Range("B1", "T" & CurRow)
(I detest R1C1 notation and couldn't work out how to use it here.)
This has worked for me, assuming your data set starts in cell A1:
Dim MyRange As Range
Dim lrow As Long
Dim lcol As Long
lrow = ActiveSheet.Cells(Application.Rows.Count, 1).End(xlUp).Row
lcol = ActiveSheet.Cells(1, Application.Columns.Count).End(xlToLeft).Column
Set MyRange = ActiveSheet.Cells(1, 1).Resize(lrow, lcol)
Good luck!