VBA to create pivot table - type mismatch error

bookworm121

New Member
Joined
Jun 22, 2011
Messages
39
I'm using the following code:

Code:
    Dim WSD As Worksheet
    Dim PTCache As PivotCache
    Dim PT As PivotTable
    Dim PRange As Range
    Dim FinalRow As Long
    Dim FinalCol As Long
    Set WSD = Worksheets("Sheet1")
    Set WSD2 = Worksheets("summary")
    For Each PT In WSD2.PivotTables
        PT.TableRange2.Clear
    Next PT
    FinalRow = WSD2.Cells(Application.Rows.Count, 1).End(xlUp).Row
    FinalCol = WSD2.Cells(1, Application.Columns.Count).End(xlToLeft).Column
    Set PRange = WSD2.Cells(1, 1).Resize(FinalRow, FinalCol)
    Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
    SourceData:=PRange)
    Set PT = PTCache.CreatePivotTable(TableDestination:=WSD.Cells(2, FinalCol + 2), _
    TableName:="PivotTable1")

What I want to do is to use the table from worksheet "summary", then use it to create a pivot table in "sheet1". I also want the pivot table to work on a range of data that can be changing... And I need to do with without using the offset tool (it has to all be VBA code)

Unfortunately I keep getting a type mismatch error at the line
Code:
Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
    SourceData:=PRange)
Any help would be appreciated
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Try:

Rich (BB code):
Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
    SourceData:="'" & PRange.Parent.Name & "'!" & PRange.Address(True, True, xlR1C1))

You should als be able to set PRange like this:

Rich (BB code):
Set PRange = WSD2.Cells(1, 1).CurrentRegion
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top