how to speed up slicer vba code selection











up vote
0
down vote

favorite












hope everyone is well.



Ive got this code that makes a slicer selection (first item):



Sub test()


Dim sc As SlicerCache

Set sc = ActiveWorkbook.SlicerCaches("Slicer_book1")


On Error GoTo errHandler
Application.ScreenUpdating = False
Application.EnableEvents = False

For Each pt In sc.PivotTables
pt.ManualUpdate = True
Next pt

With ActiveWorkbook.SlicerCaches("Slicer_book1")
.ClearManualFilter
cnt = .SlicerItems.Count
If cnt > 1 Then
For i = 2 To cnt
.SlicerItems(i).Selected = False
Next
End If
End With

For Each pt In sc.PivotTables
pt.ManualUpdate = False
Next pt

exitHandler:
Application.ScreenUpdating = True
Application.EnableEvents = True
Exit Sub

errHandler:
MsgBox ("Error in updating slicer filters.")
Resume exitHandler


somehow, this code works, but it is incedibly show. So, I was wondering whether anyone has any advice re how to properly speed this up?



Thanks and regards.










share|improve this question
























  • This question might better fit to Code Review Stack Exchange.
    – Pᴇʜ
    2 days ago















up vote
0
down vote

favorite












hope everyone is well.



Ive got this code that makes a slicer selection (first item):



Sub test()


Dim sc As SlicerCache

Set sc = ActiveWorkbook.SlicerCaches("Slicer_book1")


On Error GoTo errHandler
Application.ScreenUpdating = False
Application.EnableEvents = False

For Each pt In sc.PivotTables
pt.ManualUpdate = True
Next pt

With ActiveWorkbook.SlicerCaches("Slicer_book1")
.ClearManualFilter
cnt = .SlicerItems.Count
If cnt > 1 Then
For i = 2 To cnt
.SlicerItems(i).Selected = False
Next
End If
End With

For Each pt In sc.PivotTables
pt.ManualUpdate = False
Next pt

exitHandler:
Application.ScreenUpdating = True
Application.EnableEvents = True
Exit Sub

errHandler:
MsgBox ("Error in updating slicer filters.")
Resume exitHandler


somehow, this code works, but it is incedibly show. So, I was wondering whether anyone has any advice re how to properly speed this up?



Thanks and regards.










share|improve this question
























  • This question might better fit to Code Review Stack Exchange.
    – Pᴇʜ
    2 days ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











hope everyone is well.



Ive got this code that makes a slicer selection (first item):



Sub test()


Dim sc As SlicerCache

Set sc = ActiveWorkbook.SlicerCaches("Slicer_book1")


On Error GoTo errHandler
Application.ScreenUpdating = False
Application.EnableEvents = False

For Each pt In sc.PivotTables
pt.ManualUpdate = True
Next pt

With ActiveWorkbook.SlicerCaches("Slicer_book1")
.ClearManualFilter
cnt = .SlicerItems.Count
If cnt > 1 Then
For i = 2 To cnt
.SlicerItems(i).Selected = False
Next
End If
End With

For Each pt In sc.PivotTables
pt.ManualUpdate = False
Next pt

exitHandler:
Application.ScreenUpdating = True
Application.EnableEvents = True
Exit Sub

errHandler:
MsgBox ("Error in updating slicer filters.")
Resume exitHandler


somehow, this code works, but it is incedibly show. So, I was wondering whether anyone has any advice re how to properly speed this up?



Thanks and regards.










share|improve this question















hope everyone is well.



Ive got this code that makes a slicer selection (first item):



Sub test()


Dim sc As SlicerCache

Set sc = ActiveWorkbook.SlicerCaches("Slicer_book1")


On Error GoTo errHandler
Application.ScreenUpdating = False
Application.EnableEvents = False

For Each pt In sc.PivotTables
pt.ManualUpdate = True
Next pt

With ActiveWorkbook.SlicerCaches("Slicer_book1")
.ClearManualFilter
cnt = .SlicerItems.Count
If cnt > 1 Then
For i = 2 To cnt
.SlicerItems(i).Selected = False
Next
End If
End With

For Each pt In sc.PivotTables
pt.ManualUpdate = False
Next pt

exitHandler:
Application.ScreenUpdating = True
Application.EnableEvents = True
Exit Sub

errHandler:
MsgBox ("Error in updating slicer filters.")
Resume exitHandler


somehow, this code works, but it is incedibly show. So, I was wondering whether anyone has any advice re how to properly speed this up?



Thanks and regards.







excel vba excel-vba






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago

























asked 2 days ago









RVH

328




328












  • This question might better fit to Code Review Stack Exchange.
    – Pᴇʜ
    2 days ago


















  • This question might better fit to Code Review Stack Exchange.
    – Pᴇʜ
    2 days ago
















This question might better fit to Code Review Stack Exchange.
– Pᴇʜ
2 days ago




This question might better fit to Code Review Stack Exchange.
– Pᴇʜ
2 days ago












1 Answer
1






active

oldest

votes

















up vote
0
down vote













I prefer to use the pivot table name than slicer.



Sub test()


Dim sc As SlicerCache
Dim SIName As String
Dim pt As PivotTable, PTF As PivotField

Set sc = ActiveWorkbook.SlicerCaches("Slicer_book1")


On Error GoTo errHandler
Application.ScreenUpdating = False
Application.EnableEvents = False

For Each pt In sc.PivotTables
pt.ManualUpdate = True
Next pt

With ActiveWorkbook.SlicerCaches("Slicer_book1")
'.ClearManualFilter
cnt = .SlicerItems.Count
If cnt > 1 Then
SIName = .SlicerItems(1).Name
End If
End With

'Pivot is the sheet name where the pivot table is located
ActiveWorkbook.Worksheets("Pivot").Activate
Set pt = ActiveWorkbook.Worksheets("Pivot").PivotTables("NameOfPivotTable")
'Book is pivot field
Set PTF = pt.PivotFields("Book")
PTF.ClearAllFilters
PTF.CurrentPage = SIName

For Each pt In sc.PivotTables
pt.ManualUpdate = False
Next pt

exitHandler:
Application.ScreenUpdating = True
Application.EnableEvents = True
Exit Sub

errHandler:
MsgBox ("Error in updating slicer filters.")
Resume exitHandler

End Sub


Hope this help






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373287%2fhow-to-speed-up-slicer-vba-code-selection%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    I prefer to use the pivot table name than slicer.



    Sub test()


    Dim sc As SlicerCache
    Dim SIName As String
    Dim pt As PivotTable, PTF As PivotField

    Set sc = ActiveWorkbook.SlicerCaches("Slicer_book1")


    On Error GoTo errHandler
    Application.ScreenUpdating = False
    Application.EnableEvents = False

    For Each pt In sc.PivotTables
    pt.ManualUpdate = True
    Next pt

    With ActiveWorkbook.SlicerCaches("Slicer_book1")
    '.ClearManualFilter
    cnt = .SlicerItems.Count
    If cnt > 1 Then
    SIName = .SlicerItems(1).Name
    End If
    End With

    'Pivot is the sheet name where the pivot table is located
    ActiveWorkbook.Worksheets("Pivot").Activate
    Set pt = ActiveWorkbook.Worksheets("Pivot").PivotTables("NameOfPivotTable")
    'Book is pivot field
    Set PTF = pt.PivotFields("Book")
    PTF.ClearAllFilters
    PTF.CurrentPage = SIName

    For Each pt In sc.PivotTables
    pt.ManualUpdate = False
    Next pt

    exitHandler:
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Exit Sub

    errHandler:
    MsgBox ("Error in updating slicer filters.")
    Resume exitHandler

    End Sub


    Hope this help






    share|improve this answer

























      up vote
      0
      down vote













      I prefer to use the pivot table name than slicer.



      Sub test()


      Dim sc As SlicerCache
      Dim SIName As String
      Dim pt As PivotTable, PTF As PivotField

      Set sc = ActiveWorkbook.SlicerCaches("Slicer_book1")


      On Error GoTo errHandler
      Application.ScreenUpdating = False
      Application.EnableEvents = False

      For Each pt In sc.PivotTables
      pt.ManualUpdate = True
      Next pt

      With ActiveWorkbook.SlicerCaches("Slicer_book1")
      '.ClearManualFilter
      cnt = .SlicerItems.Count
      If cnt > 1 Then
      SIName = .SlicerItems(1).Name
      End If
      End With

      'Pivot is the sheet name where the pivot table is located
      ActiveWorkbook.Worksheets("Pivot").Activate
      Set pt = ActiveWorkbook.Worksheets("Pivot").PivotTables("NameOfPivotTable")
      'Book is pivot field
      Set PTF = pt.PivotFields("Book")
      PTF.ClearAllFilters
      PTF.CurrentPage = SIName

      For Each pt In sc.PivotTables
      pt.ManualUpdate = False
      Next pt

      exitHandler:
      Application.ScreenUpdating = True
      Application.EnableEvents = True
      Exit Sub

      errHandler:
      MsgBox ("Error in updating slicer filters.")
      Resume exitHandler

      End Sub


      Hope this help






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        I prefer to use the pivot table name than slicer.



        Sub test()


        Dim sc As SlicerCache
        Dim SIName As String
        Dim pt As PivotTable, PTF As PivotField

        Set sc = ActiveWorkbook.SlicerCaches("Slicer_book1")


        On Error GoTo errHandler
        Application.ScreenUpdating = False
        Application.EnableEvents = False

        For Each pt In sc.PivotTables
        pt.ManualUpdate = True
        Next pt

        With ActiveWorkbook.SlicerCaches("Slicer_book1")
        '.ClearManualFilter
        cnt = .SlicerItems.Count
        If cnt > 1 Then
        SIName = .SlicerItems(1).Name
        End If
        End With

        'Pivot is the sheet name where the pivot table is located
        ActiveWorkbook.Worksheets("Pivot").Activate
        Set pt = ActiveWorkbook.Worksheets("Pivot").PivotTables("NameOfPivotTable")
        'Book is pivot field
        Set PTF = pt.PivotFields("Book")
        PTF.ClearAllFilters
        PTF.CurrentPage = SIName

        For Each pt In sc.PivotTables
        pt.ManualUpdate = False
        Next pt

        exitHandler:
        Application.ScreenUpdating = True
        Application.EnableEvents = True
        Exit Sub

        errHandler:
        MsgBox ("Error in updating slicer filters.")
        Resume exitHandler

        End Sub


        Hope this help






        share|improve this answer












        I prefer to use the pivot table name than slicer.



        Sub test()


        Dim sc As SlicerCache
        Dim SIName As String
        Dim pt As PivotTable, PTF As PivotField

        Set sc = ActiveWorkbook.SlicerCaches("Slicer_book1")


        On Error GoTo errHandler
        Application.ScreenUpdating = False
        Application.EnableEvents = False

        For Each pt In sc.PivotTables
        pt.ManualUpdate = True
        Next pt

        With ActiveWorkbook.SlicerCaches("Slicer_book1")
        '.ClearManualFilter
        cnt = .SlicerItems.Count
        If cnt > 1 Then
        SIName = .SlicerItems(1).Name
        End If
        End With

        'Pivot is the sheet name where the pivot table is located
        ActiveWorkbook.Worksheets("Pivot").Activate
        Set pt = ActiveWorkbook.Worksheets("Pivot").PivotTables("NameOfPivotTable")
        'Book is pivot field
        Set PTF = pt.PivotFields("Book")
        PTF.ClearAllFilters
        PTF.CurrentPage = SIName

        For Each pt In sc.PivotTables
        pt.ManualUpdate = False
        Next pt

        exitHandler:
        Application.ScreenUpdating = True
        Application.EnableEvents = True
        Exit Sub

        errHandler:
        MsgBox ("Error in updating slicer filters.")
        Resume exitHandler

        End Sub


        Hope this help







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 2 days ago









        adhy wijaya

        42817




        42817






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373287%2fhow-to-speed-up-slicer-vba-code-selection%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

            How to fix TextFormField cause rebuild widget in Flutter