Automatic Pivot Table Refresh

A super annoying thing about Excel is that pivot tables require manual refreshes. This can easily be forgotten by a user that updates source data in a workbook and expects everything else dependent on that data to update as well. To solve this problem requires just a little VBA. This is pretty redundant but easy enough to implement and will 100% ensure the problem of missing a manual pivot table refresh never comes back.

You might have to work with a spreadsheet that has filters applied manually. In this case I didn’t find a programmatic solution to pivot table filtering so I set up an alert to tell the user (ME) that the maximum month found in the pivot table doesn’t match the current month, thus notifying me of the need to manually add the month that was not included in the pivot table.

Private Sub Workbook_Open()
    Sheets("Sheet1").Select
    ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
    Call pivotAlert
End Sub


Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Sheets("Sheet1").Select
    ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
    Call pivotAlert
End Sub


Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Sheets("Vertical View").Select
    ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
    Call pivotAlert
End Sub

Private Sub pivotAlert()
    Dim maxdate As Date
   
    For i = 6 To 25
        If Cells(i, 1) = "Grand Total" Then
            Exit For
        End If
       
        maxdate = Cells(i, 1)
    Next i
   
    todaymonth = Month(Date)
    maxmonth = Month(maxdate)
   
    If todaymonth <> maxmonth Then
        MsgBox ("ALERT: ADD MONTH " & todaymonth & " TO PIVOT")
    End If

End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *