I am getting a mismatch error 13 when running the code below. The whole code module has been in use and mostly unchanged for over a year, I haven’t changed anything recently, and I am the only user of this workbook.
Non-working code:
Private Sub CreateMIAPivots(MIABook As Workbook, MIASheet As Worksheet, MaxRow As Long)
Dim wksPivotSheet As Worksheet
Dim PivotRange As Range
Set wksPivotSheet = MIABook.Sheets.Add
wksPivotSheet.Name = "Summary"
wksPivotSheet.Tab.Color = RGB(255, 0, 0)
Set PivotRange = MIASheet.Range("A1:Y" & MaxRow)
With MIABook.PivotCaches.Add(xlDatabase, PivotRange) 'Error sets here
'More code below
MaxRow is variable, but I’ve checked it against a file with about 600 to about 7,000 rows, and the Range object is making a valid reference if I try to interact with it any other way.
I have reviewed some related questions here on SO:
- I am only working in 2003, and the error is different, anyway.
- I have confirmed I have no other
PivotCachesin the workbook before attempting to create a new one. - I have successfully gotten this work using the accepted solution here:Use named ranges (see follow-up code below)
Work-around code:
Private Sub CreateMIAPivots(MIABook As Workbook, MIASheet As Worksheet, MaxRow As Long)
Dim wksPivotSheet As Worksheet
Dim pc As PivotCache
Dim lVBAVer As Long
Dim PivotRange As Range
lVBAVer = CLng(Application.Version)
Set wksPivotSheet = MIABook.Sheets.Add
wksPivotSheet.Name = "Summary"
wksPivotSheet.Tab.Color = RGB(255, 0, 0)
Set PivotRange = MIASheet.Range("A1:Y" & MaxRow)
MIABook.Names.Add Name:="PivotRange", RefersTo:=PivotRange
'Using answer from https://stackoverflow.com/a/11868231/698590 (StackOverflow)
'as a guide for version checking. Original answer of
'CLng(Application.VBE.Version) did not work here.
#If lVBAVer <= 11 Then
Set pc = MIABook.PivotCaches.Add(xlDatabase, "PivotRange")
#Else
Set pc = MIABook.PivotCaches.Create(xlDatabase, "PivotRange")
#End If
With pc
'More code below
My question is now why is the above solution necessary? It appears that I am not the only one who has had perfectly working code suddenly stop while using PivotCaches like I had initially. So what is the real root cause here? What caused the code to stop functioning?
I’d like to know so, if possible, I can prevent future unexpected errors from arising. I know that the answer may lie in something I did somewhere else in the code/workbook (not referenced here), but I can’t for the life of me track down what changed.
So as you already know the problem is related to this: http://answers.microsoft.com/en-us/office/forum/office_2010-customize/pivotcache-type-mismatch-error-when-65536-rows/0827889e-b671-e011-8dfc-68b599b31bf5?msgId=4e3a2b20-7a72-e011-8dfc-68b599b31bf5
The reason appears to be that the VBA syntax/usage of pivot tables was changed from 2003 to 2007/2010.
So it uses this common syntax to set a named range in the workbook with this:
This section of code:
Is checking to see if the lVBAver is less than or equal to 11, if is less – then it uses this syntax:
If it is the new version then it uses this syntax:
Sometimes you just have to accept that Microsoft is not perfect(lol irony) and they do stupid things just like every other company. That is my theory anyways. Good Luck.