+ Reply to Thread
Results 1 to 5 of 5

For loop inside for loop starts over.

  1. #1
    Registered User
    Join Date
    01-09-2013
    Location
    Helsingborg, Sweden
    MS-Off Ver
    Excel 2010
    Posts
    47

    For loop inside for loop starts over.

    Hello.

    The macro below copys a template sheet to last in order and renames it by a list in range "AD7". Im trying to make the code skip one iteration if a sheet with the name in range allready exist to next name in range.
    The code now makes first sheet from first name in range then errors out in "cannot create a sheet with an allready existing name"

    And when i run the code and all sheets is allready created the code should just skip every iteration and end.

    Could anyone help me to what i need to change/ad in code below for it to work and not error out on when sheet with sheetname from range allready exists?

    thanks for all the help!

    Please Login or Register  to view this content.
    Cheers

  2. #2
    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: For loop inside for loop starts over.

    Quote Originally Posted by lavinius View Post
    Hello.

    The macro below copys a template sheet to last in order and renames it by a list in range "AD7". Im trying to make the code skip one iteration if a sheet with the name in range allready exist to next name in range.
    Are you saying that in your range of sheet names there are duplications?

    If not please upload your workbook so that we can see your problem in context.

    Incidentally why do you need the Goto. Why not just use
    Please Login or Register  to view this content.
    and replace the NextIteration: with End If

    Although in this example it's clear what you are doing, it's not generally regarded as good coding practice since it can lead to sloppy 'spaghetti' code which is difficult to read.
    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.

  3. #3
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434

    Re: For loop inside for loop starts over.

    The problem is you are looping through the existing sheets but adding a new one immediately after the first test. This means if the 2nd to last sheet name is a match you will get the error before testing for matching name.

    Please Login or Register  to view this content.
    Cheers
    Andy
    www.andypope.info

  4. #4
    Registered User
    Join Date
    01-09-2013
    Location
    Helsingborg, Sweden
    MS-Off Ver
    Excel 2010
    Posts
    47

    Re: For loop inside for loop starts over.

    Thanks for the reply!

    And thanks for the better option in code, have changed it now.
    There are no duplications in the range, But when i run the code it copys the template and renames it with first name in range.
    Then it copys the template again but error out with runtime error 1004. says it cannot rename the recently copied template with the name of an allready existing one.

    so for some reason im guessing its trying to rename it with first name in range. if i remove the if statement code works fine, copies up the template and renames em one for every name in range.

    I would like to build it for when the range later on gets additional names. so it could loop through (After run first time) and add only the missing sheets.

    Se example workbookfile

    Cheers
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    01-09-2013
    Location
    Helsingborg, Sweden
    MS-Off Ver
    Excel 2010
    Posts
    47

    Re: For loop inside for loop starts over.

    Thanks For that Andy!!

    Works perfect now!

    Never mind my second to last post. Didnt see your reply until after!

    Thanks for the help there!

    Rep Added for Both!

    Cheers

+ 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. [SOLVED] For Next Loop with IF statement inside does not loop
    By roxybethany in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-09-2013, 11:45 AM
  2. Changing the index of a for loop inside of the loop
    By drinkmorewine in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-03-2013, 12:19 PM
  3. [SOLVED] Loop inside a loop problem!
    By questionguy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-22-2012, 12:54 PM
  4. VBA For loop where number of iterations can be changed from inside loop
    By barrboy89 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-11-2011, 12:18 PM
  5. Loop inside a Loop
    By jhahes in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-07-2006, 02:23 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