Question : Excel Table Synchronization- Why does this code not work?

The following code works fine in another workbook.
Why des the same code not work in te attached file on sheet8?

Thank you...
Rick
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
   Dim ptDetail As PivotTable, ptSummary As PivotTable
   Dim pi As PivotItem, pf As PivotField
   Dim avarSelected()
   Dim lngIndex As Long
   Dim rngData As Range
   If Target.Name = "PivotTable7" Then
      On Error Resume Next
      Application.EnableEvents = False
      Set ptSummary = Target
      Set ptDetail = PivotTable1
      Set pf = ptSummary.PivotFields("Branch")
      ' get list of visible IDs in summary table
      For Each pi In pf.VisibleItems
      Set rngData = pi.DataRange
         If Not rngData Is Nothing Then
            ReDim Preserve avarSelected(lngIndex)
            avarSelected(lngIndex) = pi.Name
            lngIndex = lngIndex + 1
            Set rngData = Nothing
         End If
      Next pi
      ' update detail table
      With ptDetail
         .ManualUpdate = True
         With .PivotFields("Branch")
            ' need at least one visible!
            .PivotItems("" & avarSelected(0)).Visible = True
            For Each pi In .PivotItems
               pi.Visible = Not IsError(Application.Match(pi.Name, avarSelected, 0))
            Next pi
         End With
         .ManualUpdate = False
      End With
      Application.EnableEvents = True
   End If
   
End Sub
Open in New Window Select All

Answer : Excel Table Synchronization- Why does this code not work?

Hi Rick,

You need to remove error suppression here:

      'On Error Resume Next

and change this:

      Set ptDetail = PivotTable1

to this:

      Set ptDetail = Me.PivotTables("PivotTable1")

Try the code below.

Jim
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
   Dim ptDetail As PivotTable, ptSummary As PivotTable
   Dim pi As PivotItem, pf As PivotField
   Dim avarSelected()
   Dim lngIndex As Long
   Dim rngData As Range
   If Target.Name = "PivotTable7" Then
      Application.EnableEvents = False
      Set ptSummary = Target
      Set ptDetail = Me.PivotTables("PivotTable1")
      'Set ptDetail = PivotTable1
      Set pf = ptSummary.PivotFields("Branch")
      ' get list of visible IDs in summary table
      For Each pi In pf.VisibleItems
      Set rngData = pi.DataRange
         If Not rngData Is Nothing Then
            ReDim Preserve avarSelected(lngIndex)
            avarSelected(lngIndex) = pi.Name
            lngIndex = lngIndex + 1
            Set rngData = Nothing
         End If
      Next pi
      ' update detail table
      With ptDetail
         .ManualUpdate = True
         With .PivotFields("Branch")
            ' need at least one visible!
            .PivotItems("" & avarSelected(0)).Visible = True
            For Each pi In .PivotItems
               pi.Visible = Not IsError(Application.Match(pi.Name, avarSelected, 0))
            Next pi
         End With
         .ManualUpdate = False
      End With
      Application.EnableEvents = True
   End If
   
End Sub
Open in New Window Select All
Random Solutions  
 
programming4us programming4us