+ Reply to Thread
Results 1 to 6 of 6

Is there any formula that returns a Sheet Name, or a way to trap for a changed Name?

  1. #1
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Is there any formula that returns a Sheet Name, or a way to trap for a changed Name?

    I currently have a macro that runs during Worksheet_Activate() that stores the Sheet Name in a Defined Name (and changes it if the Sheet Name has changed).
    I then have a formula on a different sheet that points to that Defined Name (there are several formulas on that sheet that point to Defined Names on several sheets).
    I then have a macro that reads the Sheet Name from that formula. This macro runs during Worksheet_Calculate().

    The problem is if the user changes the Name of the worksheet by right mouse clicking the sheet tab but doesn't activate the sheet the Worksheet_Activate() macro doesn't update the Defined Name and my macro crashes trying to get a Sheet Name that doesn't exist any more.

    Is there any formula that returns a Sheet Name, or a way to trap when a user changes the Sheet Name?
    Foxguy

    Remember to mark your questions [Solved] and rate the answer(s)
    Forum Rules are Here

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Is there any formula that returns a Sheet Name, or a way to trap for a changed Name?

    You can put this formula into a cell and it will return the sheet name as long as the file has been saved at least once.

    =MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1,256)
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Is there any formula that returns a Sheet Name, or a way to trap for a changed Name?

    The CELL function can be used to return a sheet name:

    =MID(CELL("Filename",A1),FIND("]",CELL("Filename",A1))+1,255)

    Edited to add: beaten to it - my wife's using my computer and typing in formula on an iPad takes forever

  4. #4
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Is there any formula that returns a Sheet Name, or a way to trap for a changed Name?

    Thanks, that saves me having to figure out a different method.

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Is there any formula that returns a Sheet Name, or a way to trap for a changed Name?

    Heh, @Andrew, I cheat. I have a HUGE text file with all the most common "tricks" stored for quick copy/paste.

  6. #6
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Is there any formula that returns a Sheet Name, or a way to trap for a changed Name?

    I currently have a macro that runs during Worksheet_Activate() that stores the Sheet Name in a Defined Name (and changes it if the Sheet Name has changed).
    Why do you have to update the name with an Activate routine?

    If you define a simple named range Name: myCell RefersTo: =Sheet1!$A$1, it will update as the user changes things.

    To expand the idea,
    Name: mySheetName
    RefersTo: =MID(GET.CELL(32,Sheet1!$A$1), FIND("]",GET.CELL(32,Sheet1!$A$1))+1, 255)

    will update as the user changes the tab name of Sheet1.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  7. #7
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Is there any formula that returns a Sheet Name, or a way to trap for a changed Name?

    Mike;
    I don't have to update the Defined Name with the Activate method anymore.
    I originally used the Activate method so that it would not be volatile. But I overlooked the fact that the user could change the name after activating the sheet and my formula referring to that Defined Name would not work until the user changed sheets and then changed back. So whether I like it or not, I have to use a volatile function to accommodate a user changing the sheet name.

    I may still try to think of a way to do it in a non volatile way, but I've pretty much decided that it's not worth the trouble. There's only 1 formula that refers to each sheet's Defined Name, so it's not worth worrying about. I have thought about using the sheet's CodeName, but I don't like having the CodeName visible where a user might see it, and it's just not worth trying to hide it in a way that would prevent the user from ever seeing it.

+ 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