Excel macro refreshing multiple DCT

dlan75
dlan75 used Ask the Experts™
on
Hi all,
I have an excel spreadsheet containing around 15 excel DCT.
On that sheet, there is a drop down menu allowing selecting a username, and when changing the value, a macro updates all the DTC present on the spreadsheet with the username and refreshes the data.
Problem is that now for any reason, it crashes excel except on high perf computer.
I don't know what to do

Here is the macro :

Dim pt As PivotTable
Dim pi As PIVOTITEM
Dim strField As String

strField = "Cslts"

On Error Resume Next
'Application.EnableEvents = False
'Application.ScreenUpdating = False

    If Target.Address = Range("H1").Address Then
       
       
            For Each pt In ActiveSheet.PivotTables
                With pt.PageFields(strField)
                    For Each pi In .PivotItems
                        If pi.Value = Target.Value Then
                            .CurrentPage = Target.Value
                    Exit For
                       
                        Else
                            .CurrentPage = "(blank)"
                               
                        End If
                    Next pi
                End With
            Next pt
   
    End If

End Sub

Regards,
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mike in ITIT System Administrator
Distinguished Expert 2017

Commented:
have you tried stepping through the code in break mode to see where the problem is? Also you should place code in a code block like this:
Dim pt As PivotTable
Dim pi As PIVOTITEM
Dim strField As String

strField = "Cslts"

On Error Resume Next
'Application.EnableEvents = False
'Application.ScreenUpdating = False

    If Target.Address = Range("H1").Address Then
        
        
            For Each pt In ActiveSheet.PivotTables
                With pt.PageFields(strField)
                    For Each pi In .PivotItems
                        If pi.Value = Target.Value Then
                            .CurrentPage = Target.Value
                    Exit For
                        
                        Else
                            .CurrentPage = "(blank)"
                               
                        End If
                    Next pi
                End With
            Next pt
    
    End If

End Sub

Open in new window

Ok found a solution on another forum, code had to be optimized as :
Private Sub Worksheet_Change(ByVal Target As Range)
Dim pt As PivotTable
Dim pi As PivotItem
Dim strField As String

    strField = "Cslts"

    Application.EnableEvents = False    'This avoids the Worksheet event from restarting again everytime the PivotTable page is changed.
    Application.ScreenUpdating = False

    If Target.Address = "$H$1" Then
        For Each pt In Target.Worksheet.PivotTables
            With pt.PageFields(strField)
                .EnableMultiplePageItems = False
                Set pi = Nothing                    'Initialize Item object
                On Error Resume Next                'This will avoid an error if the item is not present
                Set pi = .PivotItems(Target.Value2) 'No need to loop through the items just set the required item  
                On Error GoTo 0                     'Clears the Error Resume
                If Not (pi Is Nothing) Then         'Validate the item 
                    .CurrentPage = Target.Value2
                Else
                    .CurrentPage = "(blank)"

    End If: End With: Next: End If

    Application.EnableEvents = True
    Application.ScreenUpdating = True

    End Sub

Open in new window

this solved my problem

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial