+ Reply to Thread
Results 1 to 9 of 9

For without Next error

  1. #1
    Registered User
    Join Date
    04-11-2013
    Location
    NYC
    MS-Off Ver
    Excel 2007
    Posts
    5

    For without Next error

    Hi,

    I'm very new to VBA so this must be a very basic question. I've been trying for about 10 hours to get a simple Loop correct, which includes an If condition, but keeps returning an error saying 'For without Next'. Any help would be appreciated. Code below:
    Please Login or Register  to view this content.
    Last edited by vlady; 04-12-2013 at 01:06 AM. Reason: code tags

  2. #2
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: For without Next error

    Please Login or Register  to view this content.
    Elegant Simplicity............. Not Always

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: For without Next error

    Hi, abhilashgm,

    the command Next j shouldn´t be placed inside an If-Statement when the Start of the loop is outside that statement, maybe try it like this:
    Please Login or Register  to view this content.
    Please note that you should put Code-Tags around the procedure for the future.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  4. #4
    Registered User
    Join Date
    04-11-2013
    Location
    NYC
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: For without Next error

    Thanks Andy and Holger for the quick replies! I moved the "Next j" outside then If loop as per your suggestions, but now I get a 'Run-time error 1004: Application-defined or object-defined error'. Does this mean a problem with the way I've entered the IsError or Search functions??

    My revised code is below (note I've changed If condition to equal False rather than True):

    Please Login or Register  to view this content.
    Last edited by abhilashgm; 04-11-2013 at 02:09 PM. Reason: Inserting code tags.

  5. #5
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: For without Next error

    Replace that line
    If Application.WorksheetFunction.IsError(Application.WorksheetFunction.Search(Cells(j, 9), Cells(i, 2))) = False Then
    with this

    Please Login or Register  to view this content.

  6. #6
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: For without Next error

    Hi, abhilashgm,

    you should use only WorksheetFunction.IsError(... and not the Application before that (sorry, missed that ion my post).

    Ciao,
    Holger

  7. #7
    Registered User
    Join Date
    04-11-2013
    Location
    NYC
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: For without Next error

    Hi Andy,

    I think that solved all error issues, but I'm still not having success because when I execute the sub, Excel just freezes after 5 seconds. I guess this is not a fault with the code, but rather my CPU...

    Thanks for your help anyway,
    Abhilash

  8. #8
    Valued Forum Contributor jwright650's Avatar
    Join Date
    12-10-2010
    Location
    Va, USA
    MS-Off Ver
    Excel 2003, Excel 2010
    Posts
    606

    Re: For without Next error

    Quote Originally Posted by abhilashgm View Post
    Hi,

    I'm very new to VBA so this must be a very basic question. I've been trying for about 10 hours to get a simple Loop correct, which includes an If condition, but keeps returning an error saying 'For without Next'. Any help would be appreciated.
    One thing that helps me when looking for those pesky "for without next errors" is some amount of indenting when writing code. I don't think there is a standard amount of indention, but indention is very helpful to keep your If, EndIf's together, For and Nexts together Do until and Loops together.....etc....
    Keep this in mind and I bet you will cut down on these time consuming, tough to find problems. Good luck with your writing.
    Life is like a roll of toilet paper. The closer it gets to the end, the faster it goes.
    John Wright

  9. #9
    Forum Contributor
    Join Date
    01-13-2013
    Location
    Oklahoma
    MS-Off Ver
    Office 2007 / Office 2010
    Posts
    123

    Re: For without Next error

    If that is the actual code you're running, then you've got an endless loop. i never increments beyond 2 so it never gets to 42000. You need to increment i before your loop line.

+ 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