Internet Explorer Breaks Excel Pivot Tables
Yep, it’s true. It appears that one hand of Microsoft doesn’t know what the other is doing. I haven’t found a lot of hits on this but apparently both sides of this bug confirm its existence and both claim it’s not their problem.
Symptom:
- Microsoft Excel cannot find the references inside a pivot table that references the same workbook when opened from Internet Explorer
Cause:
- Internet Explorer adds ‘[X]‘ to the end of the file name when storing the file in the IE temp folder, where X represents an incrementing integer based on the number of downloads of the same file, and the square brackets are illegal characters for file names in Windows
Steps to reproduce:
- Browsing with Internet Explorer
- Clicking on a link to an Excel spreadsheet with a pivot table in it that references itself
- Clicking on "Open" rather than "Save"
The only solution I could think of was to write a VBA macro to rename the file and reattach all of the pivot tables. I hope this saves someone else some trouble in the future. When you think of me, think of me fondly…
( You’ll have to add a reference to Microsoft VBScript Regular Expressions 5.5 or better. )
Sub Auto_Open()
Dim sFilename As String ' the corrected filename
Dim oASheet As Worksheet ' the worksheet object
Dim oPT As PivotTable ' the pivot table object
Dim sTempSource As String ' the pivot table temporary source variable
Dim sNewRange As String ' the new pivot table source variable
Dim reg As New RegExp ' a regular expression for matching wildcards
Dim i As Integer ' looping integer
Dim j As Integer ' looping integer
' Set a regex to test whether this is a temp spreadsheet from IE.
' Only a temp spreadsheet from IE will have square brackets in the filename.
reg.Pattern = "[[\]]"
If reg.Test(ActiveWorkbook.FullName) Then
' the temp filename has square brackets in it
' just a little message telling the user what's going on
MsgBox "Renaming temp file and reattaching pivot tables..."
' The .Name property already has the brackets stripped out of it so
' we can build the new path using it and the .Path property.
sFilename = ActiveWorkbook.Path & "\" & ActiveWorkbook.Name
' Now we save the file so that the assignment of source data to the
' pivot tables won't fail.
ActiveWorkbook.SaveAs Filename:=sFilename
' The range for the pivot table requires a square bracket
' just before the file name so we rebuild the path, less the drive letter.
sNewRange = Replace(ActiveWorkbook.Path, "C:", "") & "\[" & ActiveWorkbook.Name & "]"
' now set the regex for the tail end of the spreadsheet, including the trailing "]"
reg.IgnoreCase = True
reg.Pattern = ".*\.xls\]"
' now loop through all of the sheets looking for pivot tables
For j = 1 To ActiveWorkbook.Worksheets.Count
Set oASheet = ActiveWorkbook.Worksheets(j)
' now loop through any pivot tables on the worksheet
For i = 1 To oASheet.PivotTables.Count
Set oPT = oASheet.PivotTables(i)
sTempSource = oPT.SourceData
' go ahead and replace the invalid path with the new path
sTempSource = "'" & reg.Replace(sTempSource, sNewRange)
'oPT.PivotTableWizard SourceType:=xlDatabase, SourceData:=sTempSource
' assign the new path to the sourcedata property
oPT.SourceData = sTempSource
' release the object
Set oPT = Nothing
Next i
' release the object
Set oASheet = Nothing
Next j
' Save the workbook, just to be thorough.
' Without saving a user would be able to close the workbook without saving
' and then open the workbook from the
' recent documents list and have broken pivot tables.
ActiveWorkbook.Save
Else
' Not a spreadsheet with square brackets in the filename
'MsgBox "No renaming required"
End If
End Sub

Is this VBA script run from the Excel file or do we put it in our web code?
Comment by Joel — February 18, 2008 @ 12:13 pm
Yes, this is a VBA script. Putting this code in the document’s ThisWorkbook will force it to automatically run when the spreadsheet is opened.
Comment by admin — February 19, 2008 @ 12:09 pm
Hey, thanks for this script!
Comment by Mike Akers — August 4, 2008 @ 5:19 pm
Thanks for the script. It works nicely! (Had to replace backticks with quotes)
Comment by Greg Sheremeta — October 27, 2008 @ 3:36 pm
Yeah, Wordpress changes the single quotes into backticks to protect MySQL from SQL injection attacks. Glad it worked for you!
Jacob
Comment by admin — October 27, 2008 @ 5:21 pm
Awesome…thank you so much. Struggled with this issue for days.
Comment by Aruna Datla — February 24, 2009 @ 3:19 pm
In which document do i need to write this script? Is in code file(aspx page as we write like javascript) or seperate document (if seperate documnet do i need to save file with vba extension)?
Thanks in advance.
Comment by Sudharshan — March 24, 2009 @ 8:18 am
@Sudharshan – This code isn’t intended for spreadsheets assembled in aspx but rather for spreadsheets linked for download on pages. I’m not sure how you would create a macro for load on a dynamically built xls. Maybe someone out there knows?
Comment by admin — March 25, 2009 @ 10:14 pm
And what with xml worksheets, that cannot contain macsros???
Comment by Pawel — July 10, 2009 @ 10:51 am
@Pawel – Sorry, as I said in a previous comment, this was the only solution I could find.
Comment by admin — July 11, 2009 @ 8:08 pm
Thanks a lot. It worked.
I added the given script in to Excel workbook. It worked as expected. Excellent
Comment by Sriram — May 28, 2010 @ 3:14 pm