+ Reply to Thread
Results 1 to 13 of 13

Macro has to run twice to change correct pivot items

Hybrid View

fratello Macro has to run twice to... 07-27-2012, 11:05 PM
Mordred Re: Macro has to run twice to... 07-27-2012, 11:14 PM
fratello Re: Macro has to run twice to... 07-27-2012, 11:25 PM
Mordred Re: Macro has to run twice to... 07-27-2012, 11:41 PM
fratello Re: Macro has to run twice to... 07-27-2012, 11:52 PM
MarvinP Re: Macro has to run twice to... 07-27-2012, 11:58 PM
fratello Re: Macro has to run twice to... 07-28-2012, 12:14 AM
JosephP Re: Macro has to run twice to... 07-28-2012, 02:52 AM
fratello Re: Macro has to run twice to... 07-28-2012, 05:22 AM
MarvinP Re: Macro has to run twice to... 07-28-2012, 09:57 AM
fratello Re: Macro has to run twice to... 07-28-2012, 12:19 PM
Cutter Re: Macro has to run twice to... 07-28-2012, 03:09 PM
fratello Re: Macro has to run twice to... 07-28-2012, 10:58 PM
  1. #1
    Registered User
    Join Date
    07-27-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    11

    Macro has to run twice to change correct pivot items

    hi guys,
    I am new to vba and tried to create a macro which changes the pivot items on a specific pivot table based on the inputs in 2 cells (range called product1 or product2).
    However, the code only works if i run the macro twice. I read that it could have something to do with refreshing the worksheet, but the codes i found online did not work for me.
    The data for the pivot table comes from another worksheet within the same workbook, so no external connection.

    Sub newproduct()
    '
    On Error Resume Next
        Dim pt As PivotTable
        Dim pi As PivotItem
    
    Sheets("pivot").Select  
        Set pt = ActiveSheet.PivotTables("PivotTable1")
         
        For Each pi In pt.PivotFields("Product").PivotItems
         pi.Visible = True
      'if the criterias are shown in the range, make pivot item visible
            If pi.Name = Range("product1") Or pi.Name = Range("product2") Then
                pi.Visible = True
               
       'if item not shown in the range, deselect
            Else
                pi.Visible = False
            End If
        Next pi
    end sub

    I suppose there are errors in my code. hope someone can help me out.
    Thanks.
    greetings,
    michael
    Last edited by fratello; 07-27-2012 at 11:11 PM.

  2. #2
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Macro has to run twice to change correct pivot items

    So are you wanting this to run twice? If so, try (not tested):
    Sub newproduct()
    '
    On Error Resume Next
        Dim pt As PivotTable
        Dim pi As PivotItem
        Dim x as Long
    Sheets("pivot").Select  
        Set pt = ActiveSheet.PivotTables("PivotTable1")
        for x = 1 to 2
           For Each pi In pt.PivotFields("Product").PivotItems
            pi.Visible = True
            'if the criterias are shown in the range, make pivot item visible
            If pi.Name = Range("product1") Or pi.Name = Range("product2") Then
                pi.Visible = True
               
           'if item not shown in the range, deselect
           Else
                pi.Visible = False
           End If
           Next pi
        next x
    end sub
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  3. #3
    Registered User
    Join Date
    07-27-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Macro has to run twice to change correct pivot items

    Hi Mordred,
    Actually, i am trying to find out what is wrong with my code because it only works if the macro is run twice. So what i did was insert my code into the module twice and then it works. But your code looks much neater because you use the long function.
    Thank you very much for that, it works perfectly and i will use ur code for now!

    Do you also know wy my code has to run twice to give the correct answer?

  4. #4
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Macro has to run twice to change correct pivot items

    I'm not sure why you would have to run it twice but perhaps it is due to your
    If pi.Name = Range("product1") Or pi.Name = Range("product2") Then
    Maybe that line could be split like
    If pi.Name = Range("product1") Then
    ...
    ElseIf pi.Name = Range("product2") Then
    ...
    Else
    ...
    End If
    The whole thing without a for loop
    Sub newproduct()
    '
    On Error Resume Next
        Dim pt As PivotTable
        Dim pi As PivotItem
    
    Sheets("pivot").Select  
        Set pt = ActiveSheet.PivotTables("PivotTable1")
         
        For Each pi In pt.PivotFields("Product").PivotItems
         pi.Visible = True
      'if the criterias are shown in the range, make pivot item visible
            If pi.Name = Range("product1") Then 
                pi.Visible = True
            ElseIf pi.Name = Range("product2") Then
                pi.Visible = True 
       'if item not shown in the range, deselect
            Else
                pi.Visible = False
            End If
        Next pi
    end sub
    I don't know for sure if this will work but give it a test. If it doesn't then go with my last post's code sample.

  5. #5
    Registered User
    Join Date
    07-27-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Macro has to run twice to change correct pivot items

    ok great i will try it out when i get home. thanks a lot for ur time and help!

  6. #6
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,451

    Re: Macro has to run twice to change correct pivot items

    I'd try to put a line of code at the end of
    ActiveWorkbook.RefreshAll
    to see if that keeps you from running it twice.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  7. #7
    Registered User
    Join Date
    07-27-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Macro has to run twice to change correct pivot items

    thanks marvinp, i saw that in a similar thread and already tried it out. but it didnt work in my case strangely

  8. #8
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Macro has to run twice to change correct pivot items

    you could remove the on error resume next to find out what is actually going wrong
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  9. #9
    Registered User
    Join Date
    07-27-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Macro has to run twice to change correct pivot items

    Quote Originally Posted by JosephP View Post
    you could remove the on error resume next to find out what is actually going wrong

    hmm then i get a "runtime error 1004 Unable to set the Visible property of the pivot item class" when the macro comes to
    " Else pi.Visible = False" Line.

    What does that mean?

  10. #10
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,451

    Re: Macro has to run twice to change correct pivot items

    I found in Pivot Tables I could not visible = false everything. I needed to do a loop to turn all visible = true first and then turn things off. Perhaps you are trying to do this, which produces your error?

  11. #11
    Registered User
    Join Date
    07-27-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Macro has to run twice to change correct pivot items

    ok i found this solution in another thread http://www.excelforum.com/excel-prog...vot-table.html and i changed it to fit my sheets

        Dim pt As PivotTable
        Dim pi As PivotItem
        Sheets("pivot").Select
    
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Product")
        
        .ClearAllFilters
        For Each pi In .PivotItems
            Select Case pi.Name
                Case Range("product1"), Range("product2")
                    pi.Visible = True
                Case Else
                    pi.Visible = False
            End Select
        Next pi
    End With
    End Sub
    This code is quite similar to the one i had before (except that it clears all filters first?), but it seems to work after running only once.
    Thanks for help guys

  12. #12
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Macro has to run twice to change correct pivot items

    @ fratello

    Based on your last post it seems that you are satisfied with the solution(s) you've received but you haven't marked your thread as SOLVED. I'll do that for you now but please keep in mind for your future threads that Rule #9 requires you to do that yourself. If your problem has not been solved you can use Thread Tools (located above your first post) and choose "Mark this thread as unsolved".
    Thanks.

    Also, as a new member of the forum, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

  13. #13
    Registered User
    Join Date
    07-27-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Macro has to run twice to change correct pivot items

    thanks cutter u are rihgt it is solved
    Last edited by Cutter; 07-29-2012 at 08:04 AM. Reason: Removed whole post quote

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1