TheRedCardinal
Active Member
- Joined
- Jul 11, 2019
- Messages
- 252
- Office Version
- 365
- 2021
- Platform
- Windows
Hi all,
I am just playing around with creating some pivot tables from VBA.
I followed an online guide which worked perfectly when following his model, but has not worked when I tried to modify for my own purposes.
Code is here:
I get a mismatch error when I run it.
I have entered the message boxes to makes sure the variables work well and they do.
The error comes at the section labelled 'Define Pivot Cache but actually the code runs, and creates a blank pivot table in the right place, which doesn't seem to make sense to me if the error is at this point.
So I have done something silly, but can anybody spot it?
I am just playing around with creating some pivot tables from VBA.
I followed an online guide which worked perfectly when following his model, but has not worked when I tried to modify for my own purposes.
Code is here:
Code:
'Declare VariablesDim PSheet As Worksheet
Dim DSheet As Worksheet
Dim PCache As PivotCache
Dim PTable As PivotTable
Dim PRange As Range
Dim LastRow As Long
Dim LastCol As Long
'Delete Preivous Pivot Table Worksheet & Insert a New Blank Worksheet With Same Name
'On Error Resume Next
'Application.DisplayAlerts = False
'Worksheets("PivotTable").Delete
Sheets.Add Before:=ActiveSheet
ActiveSheet.Name = "PivotTable"
Application.DisplayAlerts = True
Set PSheet = Worksheets("PivotTable")
Set DSheet = Worksheets("Final Data")
'Define Data Range
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)
MsgBox LastRow
MsgBox LastCol
'Define Pivot Cache
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), _
TableName:="SalesPivotTable")
'Insert Blank Pivot Table
Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(1, 1), TableName:="SalesPivotTable")
'Insert Row Fields
With ActiveSheet.PivotTables("SalesPivotTable").PivotFields("Commodity")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("SalesPivotTable").PivotFields("Country")
.Orientation = xlRowField
.Position = 2
End With
'Insert Data Field
With ActiveSheet.PivotTables("SalesPivotTable").PivotFields("Amount")
.Orientation = xlDataField
.Position = 1
.Function = xlSum
.NumberFormat = "#,##0"
.Name = "Revenue "
End With
'Format Pivot Table
ActiveSheet.PivotTables("SalesPivotTable").ShowTableStyleRowStripes = True
ActiveSheet.PivotTables("SalesPivotTable").TableStyle2 = "PivotStyleMedium9"
End Sub
I get a mismatch error when I run it.
I have entered the message boxes to makes sure the variables work well and they do.
The error comes at the section labelled 'Define Pivot Cache but actually the code runs, and creates a blank pivot table in the right place, which doesn't seem to make sense to me if the error is at this point.
So I have done something silly, but can anybody spot it?