Hello,
I have a code shown below that allows me to change the source data of all pivot tables in a given workbook. It works fine, but I would like it to select all the data to the last row and last column in that targeted sheet. In other words, I don't want it to select blank rows and columns.
Converting the source data into a table is not an option in this instance.
Sub ChangeDataSourceForAllPivotTables()
Dim wb As Workbook
Dim ws As Worksheet
Dim pt As PivotTable
Dim sSourceData As String
On Error GoTo ErrHandler
sSourceData = "'C:\Blah\Blah\Blah\Blah\Blah\[Blah_Blah.xlsm]BLAH'!A1:v100000"
Set wb = ActiveWorkbook
For Each ws In wb.Worksheets
For Each pt In ws.PivotTables
pt.ChangePivotCache wb.PivotCaches.Create(xlDatabase, sSourceData)
pt.RefreshTable
Next pt
Next ws
ExitTheSub:
Set wb = Nothing
Set ws = Nothing
Set pt = Nothing
Exit Sub
ErrHandler:
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "Error"
Resume ExitTheSub
End Sub
Bookmarks