Hello,
I am trying to trigger a worksheet_calculate event that is triggered when a cell or range of cells are changed due to a formula changing the value. The code essentially takes the value of the changed cell (due to the cell changing by the formula) and then uses the new value for the Report Filter in a pivot table. The code is below, however, I am coming up with the Method 'Range' of object'_Worksheet' failed error. Any ideas on how I could make this work?
The pivot table and the cell being changed by a formula are located on the same worksheet.
Thanks in advance.
And the following in ThisWorkbook Object
I am trying to trigger a worksheet_calculate event that is triggered when a cell or range of cells are changed due to a formula changing the value. The code essentially takes the value of the changed cell (due to the cell changing by the formula) and then uses the new value for the Report Filter in a pivot table. The code is below, however, I am coming up with the Method 'Range' of object'_Worksheet' failed error. Any ideas on how I could make this work?
The pivot table and the cell being changed by a formula are located on the same worksheet.
Thanks in advance.
Code:
Private Sub Worksheet_Calculate()
If Worksheets("testdata").Range("L2").Value <> PrevVal Then
PrevVal = Range("L2").Value
End If
'Set the Variables to be used
Dim pt As PivotTable
Dim Field As PivotField
Dim NewCat As String
'pivot data
Set pt = Worksheets("testdata").PivotTables("PivotTable2")
Set Field = pt.PivotFields("name")
NewCat = Worksheets("testdata").Range("L2").Value
'This updates and refreshes the PIVOT table
With pt
Field.ClearAllFilters
Field.CurrentPage = NewCat
pt.RefreshTable
End With
End Sub
And the following in ThisWorkbook Object
Code:
Private Sub Workbook_Open()
PrevVal = Worksheets("testdata").Range("L2").Value
End Sub