+ Reply to Thread
Results 1 to 8 of 8

Not Exiting For Each Next Loop Correctly

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-05-2017
    Location
    Altamonte Springs, FL
    MS-Off Ver
    Office 365
    Posts
    146

    Not Exiting For Each Next Loop Correctly

    I created a loop that does run correctly, except that anything that comes after does not run. Am I missing an exit point?

        For Each pi In PTable.PivotFields("Status").PivotItems
            Select Case pi.Name
                Case Is = ""
                    pi.Visible = True
                Case Else
                    pi.Visible = False
            End Select
        Next pi

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Not Exiting For Each Next Loop Correctly

    I do not do Pivot Tables however

    As I see it you are looking at entries in the "Status" Field of a Pivot Table.

    So would an entry have a name?


    
        For Each pi In PTable.PivotFields("Status").PivotItems
            If pi= "" then
                    pi.Visible = True
             Else
                    pi.Visible = False
            EndIf
        Next
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Forum Contributor
    Join Date
    10-05-2017
    Location
    Altamonte Springs, FL
    MS-Off Ver
    Office 365
    Posts
    146

    Re: Not Exiting For Each Next Loop Correctly

    I only want to see the blank items in this particular list. The code does actually work for that function. Just doesn't want to go past the loop itself. Thank you.

  4. #4
    Forum Contributor
    Join Date
    10-05-2017
    Location
    Altamonte Springs, FL
    MS-Off Ver
    Office 365
    Posts
    146

    Re: Not Exiting For Each Next Loop Correctly

    I just realized you changed from my original. Thanks for that, and I do prefer an If Then statement. I'm not as familiar with the Select Case.

    That being said, I tried it and it does the same thing. For some reason it doesn't want to move on from there...

  5. #5
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Not Exiting For Each Next Loop Correctly

    If I understand correctly, the code you posted is a working part of larger code, and your problem is with code below that loop? We'd have to see more of the code, and if possible a sample workbook.
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  6. #6
    Forum Contributor
    Join Date
    10-05-2017
    Location
    Altamonte Springs, FL
    MS-Off Ver
    Office 365
    Posts
    146

    Re: Not Exiting For Each Next Loop Correctly

    Forgive me. I always have a reluctance to attach a sample because, like you said, it is part of a larger system of code. In this case, there is quite a lot, but I am able to manage breaking it down for just this issue. Please find it attached.

    Since I did take it out of the larger code for this sample (this sub is called to another), when the loop runs through to the last item, which I don't know what it would be, shows that it is unable to get the PivotItems property of the PivotField class.
    Attached Files Attached Files
    Last edited by jmklei0; 10-19-2020 at 03:41 PM.

  7. #7
    Forum Contributor
    Join Date
    10-05-2017
    Location
    Altamonte Springs, FL
    MS-Off Ver
    Office 365
    Posts
    146

    Re: Not Exiting For Each Next Loop Correctly

    Fixed it!

    "" is not the same as (blank).

    Just changed the case to (blank). Simple enough. Now, I can experiment with maybe doing it without a loop or simplifying it.

  8. #8
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,525

    Re: Not Exiting For Each Next Loop Correctly

    My debugging strategy would be to add a Stop statement immediately after the loop, then step through from there to see what the subsequent code is doing. Copied from the OP:
    For Each pi In PTable.PivotFields("Status").PivotItems
            Select Case pi.Name
                Case Is = ""
                    pi.Visible = True
                Case Else
                    pi.Visible = False
            End Select
        Next pi
    Stop
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. For loop exiting prematurely
    By whburling in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-29-2017, 04:11 PM
  2. [SOLVED] Having difficulty with exiting a Do loop
    By mc84excel in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-17-2014, 10:03 PM
  3. Problem with do loop - does not appear to be exiting
    By jbd99 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-29-2012, 05:17 AM
  4. correctly looping/exiting a recursive sub
    By broro183 in forum Excel Programming / VBA / Macros
    Replies: 41
    Last Post: 08-18-2010, 04:56 AM
  5. Not exiting For loop
    By gshock in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-13-2008, 03:50 PM
  6. New question about exiting a loop!
    By Canuckbulldog in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-18-2008, 08:36 AM
  7. [SOLVED] Exiting a Do Loop on error
    By Cloudfall in forum Excel General
    Replies: 7
    Last Post: 11-02-2005, 06:17 PM

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