+ Reply to Thread
Results 1 to 8 of 8

Can't figure out loops

  1. #1
    Registered User
    Join Date
    06-23-2008
    Posts
    16

    Can't figure out loops

    Hey all, I'm having a bunch of trouble getting loops working in my macros (I have a background in C++ and PHP, not very familiar with VB)

    Anyway, here is my code:
    Please Login or Register  to view this content.
    It's supposed to traverse through a row, create new worksheets named from the cell contents.

    It only runs one time. I have checked a million tutorial sites and they all list this as the proper syntax... Is there something within the statements that causes it to not function the way I would like?

    Thanks in advance.

    edit: also, the code within the loop came from http://www.mindspring.com/~tflynn/ex....html#AddSheet in case anyone recognizes it
    Last edited by kevnin; 06-27-2008 at 01:21 PM.

  2. #2
    Forum Contributor
    Join Date
    04-24-2007
    Location
    NYC
    MS-Off Ver
    2k3/2k7/2010
    Posts
    270
    Try this do while loop. Well that code requires u to be highlighting the first cell with the worksheet name.
    Please Login or Register  to view this content.
    This assumes that cell values are continuous and the first empty cell it comes up is the eof
    Last edited by slx; 06-27-2008 at 01:37 PM.

  3. #3
    Forum Contributor
    Join Date
    06-14-2008
    Posts
    153
    Hi,

    After the first worksheet is created, it becomes the active sheet and the do while loop will be evaluated there. Since all cells are empty the loop will end.
    To set focus back to your original sheet add the following line:
    Please Login or Register  to view this content.
    before
    Please Login or Register  to view this content.
    change "sheet1" to the actual name of your worksheet.

    Regards.
    Welcome to: http://www.exceldigest.com/myblog/
    "Excel help for the rest of us"

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    You could do it like this:
    Please Login or Register  to view this content.
    A number is a valid name for a sheet, BTW ...

  5. #5
    Registered User
    Join Date
    06-23-2008
    Posts
    16
    Thanks for the input everyone.
    As se1429 pointed out, when I add the new sheet it becomes the active sheet, changing my active cell. This is a big problem, because If I make the original sheet active, the activecell becomes A1. Is there a way to add a sheet without making it active?

    (also, shg's version only ran once because of the same reason)

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    (also, shg's version only ran once because of the same reason)
    Don't think so. Select cell A1 and push the Run button.

    To be more specific, the items (cells) in the collection over which the loop iterates is set when the loop starts; changing the active sheet does not change the collection. (I'm referring to my code.)
    Attached Files Attached Files
    Last edited by shg; 07-02-2008 at 11:49 AM.

  7. #7
    Registered User
    Join Date
    06-23-2008
    Posts
    16
    Yep I made a simple error and selected the wrong sheet (keep thinking that they start at 0...). Works fine now, thanks a lot!

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    You're welcome.

+ 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