I have a pivot table in sheet 2. The pivot table filter will automatically change each time I change the value in sheet 1 cell D1. However, the table only refreshes when I go back to sheet 2 and click anywhere on screen. How can I get this to change automatically?
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Set the Variables to be used
Dim pt As PivotTable
Dim Field As PivotField
Dim NewCat As String
'Set worksheet and cell
Set pt = Worksheets("Sheet2").PivotTables("PivotTable1")
Set Field = pt.PivotFields("Sum1")
NewCat = Worksheets("Sheet1").Range("D1").Value
'Update and refresh
With pt
Field.ClearAllFilters
Field.CurrentPage = NewCat
pt.Update
End With
End Sub
Note: I have also tried pt.Refresh and pt.RefreshTable
Related
this code is running well at initially.
But Error 1004 come out after few times testing.
the code break at this line
Set Field = pt.PivotFields("Rep Order#")
I have checked the field name in pivot table. it is exactly the same.
Can anyone help to have a look ?
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
'Set the Variables to be used
Dim pt As PivotTable
Dim pt2 As PivotTable
Dim Field As PivotField
Dim Field2 As PivotField
Dim pivot_item As PivotItem
Dim pivot_item2 As PivotItem
Dim NewCat As String
Dim test_val As String
'Here you amend to suit your data
Set pt = Worksheets("Backlog Analysis 2").PivotTables("PivotTable2")
Set pt2 = Worksheets("Backlog Analysis 2").PivotTables("PivotTable1")
Set Field = pt.PivotFields("Rep Order#")
Set Field2 = pt2.PivotFields("Rep Order#")
NewCat = Worksheets("Backlog Analysis 2").Range("L11").Value
'Here is the test if the input field exists
test_val = NewCat
For Each pivot_item In pt.PivotFields("Rep Order#").PivotItems
If pivot_item.Name = test_val Then
Exit For
End If
Next pivot_item
On Error Resume Next
'This updates and refreshes the PIVOT table
With pt
Field.ClearAllFilters
Field.PivotFilters.Add2 Type:=xlCaptionContains, Value1:=NewCat
pt.RefreshTable
End With
With pt2
Field2.ClearAllFilters
Field2.PivotFilters.Add2 Type:=xlCaptionContains, Value1:=NewCat
pt.RefreshTable
End With
End Sub
If the code works initially and then stops working, my first guess would be to either turn off Events or limit the code to run only when a specific range changes. Your code now we run every time anything changes in the workbook (including when your code changes the Pivot Tables).
Try adding this at the beginning of your code:
Application.EnableEvents = False
And this at the end:
Application.EnableEvents = True
Alternatively, it appears that you really only want this to run when Range("L11") changes. So, you could add a conditional at the top:
If Target.Address = "$L$11" And Sh.Name = "Backlog Analysis 2" Then
'Run Your Code
End If
I'm a novice VBA user and already posted on this issue, but had to re-do some tables in my workbook and the initial question ended up being inaccurate, hence trying again.
I have modified a code found online to work with my workbook which:
links a cell to a my pivot table filter;
updates the filter and refreshes pivot table once the cell has been updated or activated;
Works great. The challange is that there are 2 pivot tables on the same worksheet and I'd need to filter 2 tables at the same time. Also the filter data is different, so the filter should be linked to different cells, although they do change at the same time.
The code I was using is below. Now there is PivotTable1 and PivotTable2; entry to cell H6 is linked to the 1st table and H7 to the other table. Got a little overwhelmed at this point, but this should be possible within the same code, right?
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'This line stops the worksheet updating on every change, it only updates when cell
'H6 or H7 is touched
If Intersect(Target, Range("H6:H7")) Is Nothing Then Exit Sub
'Set the Variables to be used
Dim pt As PivotTable
Dim Field As PivotField
Dim NewCat As String
'Here you amend to suit your data
Set pt = Worksheets("Sheet1").PivotTables("PivotTable1")
Set Field = pt.PivotFields("Category")
NewCat = Worksheets("Sheet1").Range("H6").Value
'This updates and refreshes the PIVOT table
With pt
Field.ClearAllFilters
Field.CurrentPage = NewCat
pt.RefreshTable
End With
End Sub
Your code can be amended as follows...
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'This line stops the worksheet updating on every change, it only updates when cell
'H6 or H7 is touched
If Intersect(Target, Range("H6:H7")) Is Nothing Then Exit Sub
'Set the Variables to be used
Dim pt As PivotTable
Dim Field As PivotField
Dim vPivotTableNames As Variant
Dim vNewCats As Variant
Dim i As Long
'Assign the pivottable names to a variable
vPivotTableNames = Array("PivotTable1", "PivotTable2")
'Assign the new categories to a variable
vNewCats = Range("H6:H7").Value
'Update the pivotables
For i = LBound(vPivotTableNames) To UBound(vPivotTableNames)
Set pt = Worksheets("Sheet1").PivotTables(vPivotTableNames(i))
Set Field = pt.PivotFields("Category")
With Field
.ClearAllFilters
.CurrentPage = vNewCats(i + 1, 1)
End With
pt.RefreshTable
Next i
End Sub
Although, the For/Next loop can be re-written as follows...
'Update the pivotables
For i = LBound(vPivotTableNames) To UBound(vPivotTableNames)
With Worksheets("Sheet1").PivotTables(vPivotTableNames(i))
With .PivotFields("Category")
.ClearAllFilters
.CurrentPage = vNewCats(i + 1, 1)
End With
.RefreshTable
End With
Next i
I am trying to autofilter a pivot table based on cell values on another worksheet. What would need to happen?
I have 2 values on sheet A (C13 = location and M3 = material)
Based on those 2 values, the pivot table "stock" on sheet B should autofilter accordingly (filter location in cell M1 and filter material in cell M2)
If the values on sheet A are not in the filter (because no data), I should get an empty pivot table (no popup error).
I started off from example code found in this forum but these only included having cell value and filter on the same sheet and only one filter. I keep getting errors when I add another filter.
Thanks for the help!
Started with this:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Intersect(Target, Range("????")) Is Nothing Then Exit Sub
Dim pt As PivotTable
Dim Field As PivotField
Dim Field2 As PivotField
Dim NewCat As String
Dim NewCat2 As String
Set pt = Worksheets("B").PivotTables("overview")
Set Field = pt.PivotFields("location")
NewCat = Worksheets("A").Range("C13").Value
Set pt = Worksheets("B").PivotTables("overview")
Set Field2 = pt.PivotFields("material")
NewCat2 = Worksheets("A").Range("M3").Value
With pt
Field.ClearAllFilters
Field.CurrentPage = NewCat
pt.RefreshTable
End With
With pt
Field2.ClearAllFilters
Field2.CurrentPage = NewCat2
pt.RefreshTable
End With
End Sub
Now have this:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Dim pt As PivotTable
Dim Field As PivotField
Dim Field2 As PivotField
Dim NewCat As String
Dim NewCat2 As String
Set KeyCells = Range("T1:T2")
If Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
Exit Sub
End If
Set pt = Worksheets("stock").PivotTables("overview")
Set Field = pt.PivotFields("location")
With pt
Field.ClearAllFilters
End With
Set pt = Worksheets("stock").PivotTables("overview")
Set Field2 = pt.PivotFields("material")
With pt
Field2.ClearAllFilters
End With
Set pt = Worksheets("stock").PivotTables("overview")
NewCat = Worksheets("stock").Range("T1").Value
With pt
Field.CurrentPage = NewCat
pt.RefreshTable
End With
Set pt = Worksheets("stock").PivotTables("overview")
NewCat2 = Worksheets("stock").Range("T2").Value
With pt
Field2.CurrentPage = NewCat2
pt.RefreshTable
End With
End Sub
I converted a range of cells to a table, called Table1. Then I inserted a pivot table and then I inserted this:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim pt As PivotTable
Set pt = ActiveSheet.PivotTables("PivotTable1")
pt.PivotCache.Refresh
End Sub
I'm getting an error 1004. The DataSource is 'Table1'
I moved the code from Worksheet_Change event, to a Sub that you can call from a button, otherwise it will fire everytime someone modifies or touches anywhere in this Sheet.
I defined the "PivotSrc_Range" that is Set-up to "Table1", and is set-up to the PivotCache, this way, everytime someone modifies or adds a data to "Table1", the PivotCache changes, and the PivotTable will be modified once you call the Sub below:
Sub UpdatePivot()
Dim sht1 As Worksheet
Dim pt As PivotTable
Dim PvtCache As PivotCache
Dim PivotSrc_Range As Range
' modify to your sheet name (I prefer not to use ActiveSheet)
Set sht1 = ThisWorkbook.Sheets("Sheet1")
' modify to your Table's name
Set PivotSrc_Range = sht1.Range("Table1")
' set the Pivot Cache
Set PvtCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PivotSrc_Range, Version:=xlPivotTableVersion14)
' add this line in case the Pivot table doesn't exit >> first time running this Macro
On Error Resume Next
Set pt = sht1.PivotTables("PivotTable1")
On Error GoTo 0
If pt Is Nothing Then
' create a new Pivot Table in "Sheet1" sheet, start from Cell I1
Set pt = sht1.PivotTables.Add(PivotCache:=PvtCache, TableDestination:=sht1.Range("I1"), TableName:="PivotTable1")
Else
' just refresh the Pivot cache with the updated Range (data in Table1)
pt.ChangePivotCache PvtCache
pt.RefreshTable
End If
End Sub
I have inserted a command button on a worksheet that when pressed opens a password userform. when a password is entered this puts the cost centre number into a worksheet Executive Summary.
The code below does not automatically update the pivot table as it requires the target cell to be changed.
How can I change the following code to update every time the target cell changes?
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'This line stops the worksheet updating on every change, it only updates
'when cell G1 is touched
If Not Intersect(Target, Range("G1")) Is Nothing Then Exit Sub
'Set the Variables to be used
Dim pt As PivotTable
Dim Field As PivotField
Dim NewCat As String
'Here you amend to suit your data
Set pt = Worksheets("Executive Summary").PivotTables("ServiceCostAnalysis")
Set Field = pt.PivotFields("Cost Centre")
NewCat = Worksheets("Executive Summary").Range("G1").Value
'This updates and refreshes the PIVOT table
With pt
Field.ClearAllFilters
Field.CurrentPage = NewCat
pt.RefreshTable
End With
End Sub
Thanks
Just take Worksheet_Change instead of Worksheet_SelectionChange:
Private Sub Worksheet_Change(ByVal Target As Range)
This procedure executes every time a Cell in the Worksheet is changed.