Hi,
I am trying to make a Pivot Table in Excel 2003 using VBA and am getting a run-time error 13 Type mismatch on the red line below where I am trying to create my Pivot Cache. Any thoughts?
Sub MakePivotTable()
Dim pt As PivotTable
Dim strField As String
Dim data As Worksheet
Set data = Sheets("Data")
Dim PTOutput As Worksheet
Set PTOutput = Sheets.Add
Dim PTCache As PivotCache
Dim PRange As Range
' Find the last row with data
Dim finalRow As Long
finalRow = data.Cells(Application.Rows.count, 1).End(xlUp).row
' Find the last column with data
Dim finalCol As Long
finalCol = data.Cells(1, Application.Columns.count).End(xlToLeft).Column
' Find the range of the data
Set PRange = data.Cells(1, 1).Resize(finalRow, finalCol)
Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=PRange)
' Create the pivot table
Set pt = PTCache.CreatePivotTable(TableDestination:=PTOutput.Cells(1, 1), _
TableName:="SamplePivot")
' Define the layout of the pivot table
' Set update to manual to avoid recomputation while laying out
pt.ManualUpdate = True
' Set up the row fields
pt.RowGrand = False
pt.AddFields RowFields:=Array( _
"Opportuntiy_Area", "Triggered?", "Priority")
With pt.PivotFields("Triggered?")
.PivotItems("-1").Visible = False
.PivotItems("0").Visible = False
End With
pt.PivotFields("Triggered?").Subtotals = _
Array(False, False, False, False, False, False, False, False, False, False, False, False)
pt.PivotFields("Opportuntiy_Area"). _
Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
False, False)
' Set up the data fields
With pt.PivotFields("Triggered?")
.Orientation = xlDataField
.Function = xlCount
.Position = 1
End With
' Now calc the pivot table
pt.ManualUpdate = False
End Sub
Thanks!
Bookmarks