I'm using this code to update the source of all my pivot tables but I always get
"Run Time Error '424' object required".
The weird part is that placing the code in the function inside the loop actually works without this error so I don't think it's the function's code but the way I'm calling it. I would like to know why it's not working.
Private Sub Workbook_Open()
    For Each st In ActiveWorkbook.Worksheets
        For Each pt In st.PivotTables
            UpdatePivotSource (pt)
        Next
    Next
End Sub
Sub UpdatePivotSource(pt As PivotTable)
    Dim StartPoint As Range
    Dim NewRange As String
    Dim LastCol As Long
    Dim lastRow As Long
    Dim Data_Sheet As Worksheet
    Dim DataRange As Range
    Set Data_Sheet = ThisWorkbook.Worksheets(Split(pt.PivotCache.SourceData, "!")(0))
    Set StartPoint = Data_Sheet.Range("A1")
    LastCol = StartPoint.End(xlToRight).Column
    lastRow = StartPoint.End(xlDown).Row
    Set DataRange = Data_Sheet.Range(StartPoint, Data_Sheet.Cells(lastRow, LastCol))
    NewRange = Data_Sheet.Name & "!" & DataRange.Address(ReferenceStyle:=xlR1C1)
    pt.ChangePivotCache ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=NewRange)
End Sub