Hi,
My workbook is a bit complex. First I have a dashboard with pivot charts. Second I have an 'Overview' page where users can input data. This data feeds into pivot tables on sheets which are protected and hidden. There are 4 of these sheets and they are named
I have a VBA script to run whenever any changes are made on the Overview sheet so that these changes reflect in the pivot charts on the dashboard.
The script should register whenever any change is made on the Overview sheet, unprotect the hidden sheets, refresh the pivot tables within and then re-protect them. But its not doing so.
Please advise.
My script is attached.
I'm using version 2410 of Excel 64bit
Possible Reasons and Suggested Solutions
Event Not Triggering:
Check the Code Location: Ensure that the code is placed in the Overview worksheet's code module, not in a standard module or another worksheet. The Worksheet_Change event code must reside in the "Overview" sheet's VBA module.
Enable Events: If events are disabled globally (for example, if at some point Application.EnableEvents = False was used), the Worksheet_Change event won't fire.
Target Range Limitations:
Currently, you are checking If Not Intersect(Target, Me.Range("A1:Z100")) Is Nothing Then. If changes occur outside A1:Z100, the code will not run. Consider removing or expanding this range limit if your changes may occur elsewhere on the Overview sheet:
If Not Intersect(Target, Me.UsedRange) Is Nothing Then
' ... your code ...
End If
or simply remove the If condition if you want any change in the sheet to trigger the refresh.
Ensuring Pivot Tables Are Refreshed Before Pivot Charts:
Pivot charts typically refresh when their underlying pivot tables are refreshed. While your code calls co.Chart.PivotLayout.PivotTable.RefreshTable, it might help to explicitly refresh all pivot tables in the hidden sheets first, before the pivot charts. For example:
Dim pt As PivotTable
For Each ws In ThisWorkbook.Worksheets
If Not IsError(Application.Match(ws.Name, sheetNames, 0)) Then
For Each pt In ws.PivotTables
pt.RefreshTable
Next pt
End If
Next ws
Protection Logic:
The current logic checks if the sheets are protected before unprotecting, and then checks if they are unprotected before protecting again. This might not accurately restore the original state. Since your intent is always to unprotect, refresh, and then re-protect, you may simplify the logic:
' Unprotect all target sheets
For Each ws In ThisWorkbook.Sheets
If Not IsError(Application.Match(ws.Name, sheetNames, 0)) Then
On Error Resume Next ' in case sheet is not protected
ws.Unprotect Password:=protectionPassword
On Error GoTo 0
End If
Next ws
' Refresh pivot tables directly before refreshing charts
Dim pt As PivotTable
For Each ws In ThisWorkbook.Sheets
If Not IsError(Application.Match(ws.Name, sheetNames, 0)) Then
For Each pt In ws.PivotTables
pt.RefreshTable
Next pt
End If
Next ws
' Refresh all PivotCharts in Dashboard
Set ws = ThisWorkbook.Sheets("Dashboard")
For Each co In ws.ChartObjects
If Not co.Chart.PivotLayout Is Nothing Then
co.Chart.PivotLayout.PivotTable.RefreshTable
End If
Next co
' Re-protect all target sheets
For Each ws In ThisWorkbook.Sheets
If Not IsError(Application.Match(ws.Name, sheetNames, 0)) Then
ws.Protect Password:=protectionPassword, _
DrawingObjects:=True, Contents:=True, Scenarios:=True, _
AllowUsingPivotTables:=True
End If
Next ws
Confirming Sheet Names and Existence:
Double-check that the sheet names specified in sheetNames exactly match the actual sheet names. Even a small typo or trailing space can prevent the code from executing correctly.
Debugging Tips:
Insert MsgBox or Debug.Print statements to confirm which parts of the code are running:
MsgBox "Change detected in Overview sheet. Beginning refresh..."
Step through the code manually (F8 in the VBA editor) to see where it might be failing.
Refreshing Pivot Caches:
If all else fails, try refreshing the pivot cache at the workbook level. This ensures all pivot tables and charts tied to them are updated:
vba
Copy code
Dim pc As PivotCache
For Each pc In ThisWorkbook.PivotCaches
pc.Refresh
Next pc
Hi Catalin,
Thank you so much! It is working now, I had the module on the wrong sheet. Also, I really appreciate all the other suggestions. This will help me a lot to get better and more efficient.
Thanks again.