+ Reply to Thread
Results 1 to 7 of 7

Problem with For To..

  1. #1
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Problem with For To..

    Hi,

    I have this code as example, but when I try to run it, it says:
    "Next without For".

    Am I missing anything, or doing anything not good?

    Please Login or Register  to view this content.
    Any help?

    Thanks.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2406 Win 11 Home 64 Bit
    Posts
    24,004

    Re: Problem with For To..

    I believe that your Next j statement needs to go after your End If statement or before your If statement. Can't really tell because I don't know what your intentions for work flow are.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Problem with For To..

    Hi calvinle,

    You had the 'If Loop' wrapped around the 'Next j'.

    Your code should look something like this:
    Please Login or Register  to view this content.
    Lewis

  4. #4
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,649

    Re: Problem with For To..

    It seems that your IF statement starts within For loop but Elseif and End If are out side the For loop (i.e. after Next j).
    Regards
    sktneer


    Treat people the way you want to be treated. Talk to people the way you want to be talked to.
    Respect is earned NOT given.

  5. #5
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Problem with For To..

    Hi,

    Yes your use of the If..End If is wrong. You can't start an IF block i side a loop and End it outside the loop. In general you must have

    Please Login or Register  to view this content.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  6. #6
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Problem with For To..

    Thanks everyone for your response.. The reason I add the "j" is to not have to repeat the same macro again, so I called it "j", for the macro to redo this whole process a second time:

    Instead of having to do this:
    Please Login or Register  to view this content.
    I wanted to repeat this process 2 times without having to copy and paste this code 2 times, so I add a "j", and have to next j to go back to redo this code once again.

  7. #7
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Problem with For To..

    Quote Originally Posted by sktneer View Post
    It seems that your IF statement starts within For loop but Elseif and End If are out side the For loop (i.e. after Next j).
    Absolutely right. I did not know that we cannot Next loop before the end if...

    I solved it

    Thanks everyone!

+ 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. Replies: 6
    Last Post: 10-20-2013, 07:16 PM
  2. input box problem, cell filtering problem, result display while locking sheet
    By croozin in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-01-2013, 08:52 AM
  3. Replies: 6
    Last Post: 05-28-2013, 05:08 PM
  4. Problem Using Solver with an optimization stock problem
    By Jagrubski in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-24-2013, 03:41 AM
  5. Replies: 2
    Last Post: 01-22-2013, 07:09 AM

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