Excel macro refreshing multiple DCT

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,
LVL 12
dlan75Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike in ITIT System AdministratorCommented:
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

0
dlan75Author Commented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dlan75Author Commented:
this solved my problem
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.