0

I am trying to use VBA to insert a pivot table.

However, every time I try to run it I get an error: "Run-time error 13: Type mismatch" on the Define Pivot Cache portion.

I've tried changing the variable names as well as not using some of them, but I still get the same error. Am I missing something?

Sub PivotTableAdd()

    'Declare Variables
    Dim 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

    'Insert a New Blank Worksheet
    'On Error Resume Next
    Application.DisplayAlerts = False
    'Worksheets("Pivot").Delete
    Sheets.Add Before:=ActiveSheet
    ActiveSheet.Name = "Pivot"
    Application.DisplayAlerts = True
    Set PSheet = Worksheets("Pivot")
    Set DSheet = Worksheets("Report")

    '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)

    'Define Pivot Cache
    Set PCache = ActiveWorkbook.PivotCaches.Create _
    (SourceType:=xlDatabase, SourceData:=PRange). _
    CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), _
    TableName:="PivotTable")

    'Insert Blank Pivot Table
    Set PTable = PCache.CreatePivotTable _
    (TableDestination:=PSheet.Cells(1, 1), TableName:="PivotTable")

End Sub

Edit: I changed this line and got it to work:

Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange.Address(True, True, xlR1C1, True))

1 Answer 1

1

Change this:

Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), _
TableName:="PivotTable")

To:

Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange)

Set PTable = PCache.CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), _
                               TableName:="PivotTable")

In your original code CreatePivotTable returns a PivotTable, not a PivotCache, so giving you the "type mismatch" error

Edit: tested this and works fine for me

Sub PivotTableAdd()

        'Declare Variables
        Dim 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

        'Insert a New Blank Worksheet
        'On Error Resume Next
        Application.DisplayAlerts = False
        'Worksheets("Pivot").Delete
        Sheets.Add Before:=ActiveSheet
        ActiveSheet.Name = "Pivot"
        Application.DisplayAlerts = True
        Set PSheet = Worksheets("Pivot")
        Set DSheet = Worksheets("Report")

        '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)

        'Define Pivot Cache
       Set PCache = ActiveWorkbook.PivotCaches.Create( _
                  SourceType:=xlDatabase, SourceData:=PRange)

       Set PTable = PCache.CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), _
                               TableName:="PivotTable")
End Sub
Sign up to request clarification or add additional context in comments.

2 Comments

Tim, I made the change mentioned and I'm still getting the same error. Any ideas?
On the Set PCache line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.