+ Reply to Thread
Results 1 to 12 of 12

VBA Worksheet Tab Rename Macro Causing Excel Infinite Loop

  1. #1
    Registered User
    Join Date
    04-14-2011
    Location
    Bay Area, CA
    MS-Off Ver
    Excel 2007
    Posts
    32

    VBA Worksheet Tab Rename Macro Causing Excel Infinite Loop

    Howdy,

    So, I found the following "Worksheet Tab Rename " macro code posted on MrExel by member VOG, in the following thread:

    http://www.mrexcel.com/forum/excel-q...s-formula.html

    Please Login or Register  to view this content.
    Nobody over there seems to be able to replicate my issue or determine what is causing my issue, so I thought that I should post here in the hopes that, since I can actually attach my spreadsheet here, folks could open it & replicate my issue.

    The macro code works perfectly, except when I make a copy of the Worksheet (by right-clicking the Tab & selecting "move or copy") and then paste it in the workbook, it starts an infinite loop which I must kill Excel in order to get out of.

    I know it's the macro code that is the issue, or that is triggering the issue, because when I have the code commented out, I can make a copy of the Worksheet.

    My "A1" in the code is "S47" in my sheet, and it is calculated from a formula. Also, the cell & formula get updated when a user selects a choice from a drop-down list in a different cell. I want the user to select the choice from the drop-down list, and then that choice gets used as the name for the tab.

    If he copies the sheet, he will then select another (different) choice from the drop-down list on that sheet. Each sheet that he copies must have a different choice selected, so at no time will there be more than 1 sheet with the same choice selected (except of course on the initial "copy & paste" of the tab, which I think is causing the infinite loop).

    If anyone can chime in w/ a solution, as well as how to troubleshoot / debug this, any & all help would be much appreciated.

    Thanks,
    Rob
    Excel 2007
    Windows 7
    Attached Files Attached Files
    Last edited by rbrookov; 05-07-2014 at 03:42 PM. Reason: Added code tags as per forum rules

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VBA Worksheet Tab Rename Macro Causing Excel Infinite Loop

    No need to use the Worksheet _Calcule event procedure. That is triggered when any formula is calculated.

    Have the combox event procedure rename the sheet based on the formula cell, or create the name in the combox code.

    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    04-14-2011
    Location
    Bay Area, CA
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: VBA Worksheet Tab Rename Macro Causing Excel Infinite Loop

    AlphaFrog,

    Thank you for your assistance!! I moved the macro to the Combobox procedure, and it works!! However, even though it does NOT throw me into an infinite loop, it does give me a Run-time error 1004: Cannot rename a sheet the same name as another sheet...

    The interesting thing is that it did create a new sheet, and just added (2) to the end of it, so I don't know why it still errored out... Is there some way to implement an error check, or otherwise remove / avoid this error?

    If I click "End" on the Error message, it goes away & the copied sheet renames, everything looks good. However, if I click "Debug" - it takes me to this part of the code:

    Please Login or Register  to view this content.
    Again, thank you - I feel I am ALMOST there!!!
    Last edited by rbrookov; 05-07-2014 at 05:27 PM. Reason: Added part of code where it is creating error

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VBA Worksheet Tab Rename Macro Causing Excel Infinite Loop

    When you copy a sheet it's going to automatically name it the same name as the original with a (2) at the end of it.

    I'm guessing you still have some legacy renaming code still in the Worksheet_Calculate procedure or some other event procedure?

    I'm not exactly sure what you want to happen and the order you do things. Do you copy then rename or do you rename then copy? Do you want the (2)? What do you want if the same supplier is selected a 2nd time and the sheet is copied again.

  5. #5
    Registered User
    Join Date
    04-14-2011
    Location
    Bay Area, CA
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: VBA Worksheet Tab Rename Macro Causing Excel Infinite Loop

    I checked & do not have legacy code laying around. Is the reason I'm getting an error because of the "On Error Resume Next" or "On Error Goto 0" (admittedly, I don't really know what these 2 statements do).

    And since each sheet will be a quote we receive from a supplier, there will never be a time where there is more than 1 sheet w/ the same supplier, because the whole reason for copying the sheet is if we have more than 1 supplier who has supplied us w/ quotes - thus, each sheet would be the quote from a unique supplier, hence a unique tab name.

    As far as how the events should occur: The user opens the sheet & fills out the stuff in yellow. When the user selects the supplier via the drop-down, the tab should rename. If there is more than 1 supplier, then the user should right-click on the tab & make a copy of that sheet, and then start filling it out w/ the unique information, including selecting a different supplier, which, in turn, will rename that sheet accordingly.

    Right now, when I right-click & select "copy", it does create a 2nd sheet w/ the same name as the 1st sheet, but w/ the (2) added to it - which I'm ok with, as long as the user can overwrite that new name by selecting a different supplier from the drop-down.... The issue is that it pops up the "Cannot rename a sheet to the same name as another sheet" error, to which there are 3 options: End, Debug, and Help.

    If I click "End" then the error goes away, but I guess it kicks itself OUT of the macro, because when I choose a different supplier for the 2nd sheet, the sheet does not rename (and for that matter, Cell B22 doesn't update either). I can select a new supplier from the drop-down, but the rest of the updates do NOT occur.

    Is this something you can replicate on your end?

    And I guess I just want it to NOT pop-up the "Error" message, and to allow the updates / refreshes to occur on the new sheet.

    Thank you!
    Last edited by rbrookov; 05-07-2014 at 07:08 PM. Reason: changed from cell C10 to B22

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VBA Worksheet Tab Rename Macro Causing Excel Infinite Loop

    I can replicate what you described.

    Does the combobox text (selected supplier) always go in cell B22 ?

  7. #7
    Registered User
    Join Date
    04-14-2011
    Location
    Bay Area, CA
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: VBA Worksheet Tab Rename Macro Causing Excel Infinite Loop

    Quote Originally Posted by AlphaFrog View Post
    I can replicate what you described.

    Does the combobox text (selected supplier) always go in cell B22 ?
    Yes, it always goes in Cell B22.

  8. #8
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VBA Worksheet Tab Rename Macro Causing Excel Infinite Loop

    Start out with fresh with just the one worksheet and just this code for the Combobox1_Change.

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    04-14-2011
    Location
    Bay Area, CA
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: VBA Worksheet Tab Rename Macro Causing Excel Infinite Loop

    oh wow, thanks!! So the 1st time the user changes the 1st sheet, it pops up the msg box... is it supposed to do that?

    And I think it's popping up message boxes for the number of sheets there are - for instance, if there are 3 sheets, then by the time you've made 2 copies, it goes through the loop 2 more times & pops up 3 msg boxes, each w/ the unique name - is that the intent? If so, then that's fine, and I appreciate the help you've give me thus far - it's MUCH better than what I had

  10. #10
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VBA Worksheet Tab Rename Macro Causing Excel Infinite Loop

    Quote Originally Posted by rbrookov View Post
    oh wow, thanks!! So the 1st time the user changes the 1st sheet, it pops up the msg box... is it supposed to do that?

    And I think it's popping up message boxes for the number of sheets there are - for instance, if there are 3 sheets, then by the time you've made 2 copies, it goes through the loop 2 more times & pops up 3 msg boxes, each w/ the unique name - is that the intent? If so, then that's fine, and I appreciate the help you've give me thus far - it's MUCH better than what I had
    It should popup one of two message boxes when you change the combobox and regardless of how many sheets you've already copied.

    If the new sheet name was successful, Msgbox 1 asks if you want to make another copy.

    If the new sheet name is already used, Msgbox 2 just notifies you of that and the sheet is not renamed. This is in case you accidentally chose the wrong supplier.

  11. #11
    Registered User
    Join Date
    04-14-2011
    Location
    Bay Area, CA
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: VBA Worksheet Tab Rename Macro Causing Excel Infinite Loop

    AlphaFrog,

    OK, I get it now. I followed the code to see what was happening. I'm going to let my users beta-test it, but from what I saw when I was testing it, I think it's GTG!!!

    What I might do is just comment out the 2nd msgbox - I already know that they're gonna complain about having it pop up, especially if they have more than a few supplier quotes to enter... they'll say it's a nuisance (which, in reality, it SHOULD be a nuisance, to nag them to ensure that they change the supplier). But we'll see...

    Anyway, I really appreciate the help you've given me - and the code was commented well enough that I can understand what it was doing, which helps me to learn!!

    Thank you SO much, I really appreciate it!!

  12. #12
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VBA Worksheet Tab Rename Macro Causing Excel Infinite Loop

    You're welcome. It's yours to change now. I'm sure you'll want to at least change the messages in the message boxes.

+ 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. BeforeRightClick causing infinite loop and slow calculation
    By k64 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-28-2013, 07:18 PM
  2. Excel 2007 - Print Macro - Infinite Loop Issue
    By Fraenk in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-03-2012, 07:50 AM
  3. worksheet rename using location causing error
    By wejones in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-06-2011, 08:46 PM
  4. Macro goes into an infinite loop
    By excelforum123 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-21-2010, 10:08 AM
  5. [SOLVED] worksheet change infinite loop/calculate for user functions
    By Mike in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-17-2006, 12:30 AM

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