+ Reply to Thread
Results 1 to 5 of 5

Why does my For Loop stop at 1???

  1. #1
    Registered User
    Join Date
    08-15-2016
    Location
    Leeds, England
    MS-Off Ver
    2013
    Posts
    14

    Why does my For Loop stop at 1???

    Hi all, I've written a code to extract values from a worksheet and copy to the clipboard so the user can do what they want with the resulting string. In the file I'm working with, there's 10 expected results. I have a For Loop at the end to create one long string with all the results listed. But when I run the macro, I only get the first result in the last MsgBox, and "x" is only =1 rather than 9, which is what I expected (as it starts at 0).

    Here's my code:

    Please Login or Register  to view this content.
    The words "Declare! (>limit)" indicate that the text value in the cell 9 columns to the left (in column B) should be copied; otherwise it should be left alone.

    All the elements of my code work properly individually (I've tried them). I can find each of the allergens in the dictionary I created. If I write:
    Please Login or Register  to view this content.
    I get item 5 from the dictionary. This works for all 10.

    Why is my Loop stopping at 1 iteration? I don't get any errors.

    I have all the references loaded that I need for this code to work, so it's probably not that.

    Help?!
    Last edited by nickersonpower; 03-10-2017 at 12:00 PM.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,751

    Re: Why does my For Loop stop at 1???

    Your syntax is wrong for this line of code

    Please Login or Register  to view this content.
    You want this:

    Please Login or Register  to view this content.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,751

    Re: Why does my For Loop stop at 1???

    Here's a little more of an esoteric explanation for those who care.

    The syntax for a For is

    For variable = expression1 To expression2

    It looks like you thought it was necessary to repeat the variable after the To.

    The statement you wrote says:

    Iterate the variable x starting at the value 1 and ending at the value

    x = (allergenlist.Count - 1)

    In VBA, the = sign can be an assignment statement and it is also a compare operator that returns a Boolean value. So the expression you wrote there is evaluated and returns FALSE, because x is not equal to that value. But the For statement is looking for an integer there, so it interprets the FALSE as 0. So your For statement is effectively

    Please Login or Register  to view this content.
    So it goes through once, then quits.

  4. #4
    Registered User
    Join Date
    08-15-2016
    Location
    Leeds, England
    MS-Off Ver
    2013
    Posts
    14

    Re: Why does my For Loop stop at 1???

    Thanks so much both, it worked perfectly! I knew I could rely on you

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,751

    Re: Why does my For Loop stop at 1???

    One last thought: I suggest formatting your code using indentation to show logical structure. Makes it easier to read.

    Please Login or Register  to view this content.

+ 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. how to get a loop to stop
    By stephme55 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-26-2016, 12:48 PM
  2. VBA Loop Will Not Stop.
    By shopholic12 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-23-2015, 08:19 AM
  3. how to stop For Loop and Next
    By Anvuri in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-10-2014, 02:12 PM
  4. How to Stop a Do While Loop at a certain row??
    By jtemp57 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-27-2014, 09:53 AM
  5. [SOLVED] how to stop this loop....
    By vimalanathk in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 12-03-2013, 03:45 AM
  6. How to stop the for loop?
    By ccs1981 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-25-2008, 07:33 AM
  7. Loop until won't stop!?
    By meacho in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-27-2007, 03:37 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