+ Reply to Thread
Results 1 to 4 of 4

Next without For error in nested loop - Escaping a Nested Loop?

  1. #1
    Registered User
    Join Date
    11-18-2010
    Location
    San Francisco, CA
    MS-Off Ver
    Excel 2007
    Posts
    3

    Next without For error in nested loop - Escaping a Nested Loop?

    Hi,
    I am trying to figure out a way to run a Vlookup on a Cell in my "System File" by checking a table in a "New Data" File. HOWEVER, if there is an #N/A error, I want the cells' values to be unchanged. I've come up with the following, however, I keep getting a "Next without For" error. Is it possible to escape a nested For Next loop?

    The tl;dr semantic version:
    Please Login or Register  to view this content.
    My more or less actual code is as follows:
    Please Login or Register  to view this content.
    Thanks in advance for any insight on how to accomplish this task.

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Next without For error in nested loop - Escaping a Nested Loop?

    Hi

    If you only want to process something when there is a result, then make the if statement reflect that, and allow the loop to function normally.

    Using your pseudo code approach to the if
    for j = 1 to 3
    set x = .find(thing)
    if not X is nothing then
    do whatever
    end if
    next j
    Hopefully that will make sense.

    rylo

  3. #3
    Forum Contributor tkowal's Avatar
    Join Date
    11-20-2010
    Location
    Miami, Fl
    MS-Off Ver
    Excel 2010
    Posts
    150

    Re: Next without For error in nested loop - Escaping a Nested Loop?

    I would take rylo's suggestion and re-write your code so you would not have to exit a loop.

    --But if you have toooo ... the vba command to exit a For Loop is "Exit For" Again this is poor programming practive and leads to unmanagable code.



    Ted

  4. #4
    Registered User
    Join Date
    11-18-2010
    Location
    San Francisco, CA
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Next without For error in nested loop - Escaping a Nested Loop?

    I guess it's the logic of testing "If Not X is Nothing" is a little weird to me in that I'll have to bury tests within one anothers' If-Then loops. Still, it works, I just don't like how the code plays out. Thanks much.

+ 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