Hello
I hope someone may be able to assist.
I have VBA code (see below) on a specific worksheet that will run when a cell is changed. It will refresh several pivot tables on the single sheet and attempt to insert a row. The insert row [Selection.Insert] causes the error message:
Run time error 1004: We can't make this change for the selected cells because it will affect a PivotTable. Use the field list to change the report. If you are trying to insert or delete cells, move the PivotTable and try again.
However if I make the change on the sheet manually, then that works fine, but the VBA will not perform the same.
The code is:
________________________________________________________________________________________________________________________________
'On Specific worksheet
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = True
If Not (Intersect(Target, Me.Range("RefreshSalary")) Is Nothing) Then
Application.EnableEvents = False 'to prevent endless loop
Application.Goto Reference:="RefreshSalary"
If ActiveCell.Value = "Refresh" Then
Sheets("Summary").Unprotect Password:=""
ActiveCell.Offset(0, 0).Activate
Selection.Insert
Application.Goto Reference:="RefreshSalary"
Dim PT As PivotTable
For Each PT In ActiveSheet.PivotTables
PT.RefreshTable
Next PT
ActiveCell.Value = "Selection"
Worksheets("Summary").Columns("A:Z").AutoFit
Sheets("Summary").Protect Password:=""
End If
End If
Application.Goto Reference:="RefreshIncome"
Application.EnableEvents = True
End Sub
____________________________________________________________________________________________________________________
I know multi-pivot tables on a single sheet is not ideal but this is the users' preferred approach. I guess the VBA thinks the insert position is a pivot table whereas the manual effort doesn't.
Any help will be much appreciated.
Thank you,
Dave