+ Reply to Thread
Results 1 to 5 of 5

Excel VBA - Not looping through my while loop

  1. #1
    Forum Contributor
    Join Date
    06-15-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    100

    Excel VBA - Not looping through my while loop

    So I have a code thats pasting data into an excel column, i'm then trying to loop through the data in that column and create a new sheet from data for every value in column B but it's stopping after completing this action once and not looping through the column.

    Any ideas?

    Please Login or Register  to view this content.

  2. #2
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,911

    Re: Excel VBA - Not looping through my while loop

    Specify a sheet for the Cells(i, 2) property call.
    Rory

  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,721

    Re: Excel VBA - Not looping through my while loop

    What module is this code in? What worksheet are you looping through?

    At the end of the loop you create a new sheet, which becomes the active sheet. If this code is in a standard module (like Module1) then the condition at the top of the loop will refer to Cells(i, 2) in the active sheet, which is a different sheet than it referred to the first time through the loop. Because the sheet was just created, there is no data in column 2 and your loop will exit.

    It is a best practice to qualify range references, such as what you did in the first line of the loop.

    It is not a good practice to select thing if you are not doing so for the convenience of the user. I would rewrite the code as follows to clean up, but this doesn't solve your original question:

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

  4. #4
    Forum Contributor
    Join Date
    06-15-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    100

    Re: Excel VBA - Not looping through my while loop

    Perfect, worked a charm, thanks guys.

  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,721

    Re: Excel VBA - Not looping through my while loop

    If your question has been answered please mark your thread as "Solved" so that members will know by looking at the thread title that your problem is solved. Go to the menu immediately above your first post to the thread and click on Thread Tools. From the dropdown menu select "Mark this thread as solved..."

    If a member helped you solve your problem, consider adding to their reputation by clicking on the star icon addreputationiconsmall.jpg below their name.

+ 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. Excel vba - Getting loop to work [looping through ComboBoxes on a UserForm]
    By Ankers in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-24-2017, 10:44 AM
  2. Looping through folder to run macro on each excel file, but it DOESN'T LOOP!
    By swade730 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-16-2015, 05:55 PM
  3. Need help looping within a loop
    By joves in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 01-12-2014, 06:08 AM
  4. [SOLVED] Trouble Looping Through an Excel Range using a 2D 'For Loop' with VBA
    By Sarvazad in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-14-2013, 04:33 PM
  5. [SOLVED] VBA Loop not looping
    By balla506 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-28-2012, 12:33 PM
  6. Loop not looping
    By bg_ac_dziner in forum Excel General
    Replies: 2
    Last Post: 03-16-2010, 03:13 PM
  7. [SOLVED] Looping a loop?
    By John in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-15-2005, 10:05 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