+ Reply to Thread
Results 1 to 7 of 7

Debugging - Compile error: Invalid Next control variable reference

  1. #1
    Registered User
    Join Date
    05-27-2013
    Location
    Middle, Nowhere
    MS-Off Ver
    Excel 2003
    Posts
    13

    Debugging - Compile error: Invalid Next control variable reference

    I wrote the following code to import text data and fill in an excel database with the data from the imported text file. The text file is then deleted, a new text file imported and the cycle repeats. However I'm getting a Compile error: Invalid Next control variable reference at Next FileNameCell How can I fix this? Thanks!


    Please Login or Register  to view this content.

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Debugging - Compile error: Invalid Next control variable reference

    You have two for loops but only one next.

    The codeis executing the for i = 2 to 1000 loop and encounters next filenamecell. You need to add next i somewhere.

    I would assume you need this:
    Please Login or Register  to view this content.

  3. #3
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Debugging - Compile error: Invalid Next control variable reference

    xxgirlinbluexx,

    See the BOLD additions:

    Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

    Please Login or Register  to view this content.
    Last edited by stanleydgromjr; 06-25-2013 at 06:47 AM.
    Have a great day,
    Stan

    Windows 10, Excel 2007, on a PC.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved by clicking EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  4. #4
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Debugging - Compile error: Invalid Next control variable reference

    Stan,

    Your code will give an error, you don't need the end if because the if statement is all contained on the single line.

    I also think the next i should go before cells.select as otherwise you are just repeating the lines from cells.select to clearcontents, no need to have those in the loop.

    The code I posted in post 2 should be correct.

  5. #5
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Debugging - Compile error: Invalid Next control variable reference

    yudlugar,

    Yes, I just re-posted my code.

  6. #6
    Registered User
    Join Date
    05-27-2013
    Location
    Middle, Nowhere
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Debugging - Compile error: Invalid Next control variable reference

    Thanks guys, Can't believe I missed something so simple

  7. #7
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Debugging - Compile error: Invalid Next control variable reference

    xxgirlinbluexx,

    Thanks for the feedback.

    You are very welcome. Glad we could help.

    And, come back anytime.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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