+ Reply to Thread
Results 1 to 9 of 9

Dealing with sheets codenames

  1. #1
    Registered User
    Join Date
    02-06-2013
    Location
    Earth
    MS-Off Ver
    Excel 2003
    Posts
    12

    Dealing with sheets codenames

    Hello

    I am trying to write some code in VB that will be able to function right in some different sheets, that I dont know their name in advance (the user can duplicate sheets using a macro). First, after a long time of googling I still havent found a decent way to retrieve the active sheet Codename from which I called the function (from a button).

    Next, even if I knew the Codename of the current sheet, how do I use it inside the code, for example, replacing:

    Worksheets("somename").Range("A1").Value
    to
    [variable].Range("A1").Value ?

    Is it possible?

    thanks

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,844

    Re: Dealing with sheets codenames

    Hello sprites. It's not clear what you are looking for. Could you post a copy of your file with a detailed explanation of what you want to do? It would be easier to help if we could see what you're working with.

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,669

    Re: Dealing with sheets codenames

    In the immediate window, type: ?Activesheet.Codename

    Unless you have changed it, the codename is, for example, Sheet1 for Sheets("Sheet1"). Again, if you haven't changed anything, Sheets(4) is the same thing as Sheets("Sheet4") and the same as just Sheet4 ... assuming you have 4 sheets in your workbook.

    So, instead of referring to Sheets("Sheet4") you could refer to Sheet4, for example, Sheet4.Range("A1") or even Sheet4.[A1]

    Hope this helps clarify how you find it and how you use it.

    You can only change it in the VBE or in code but, I believe, changing it in VBA is possible but not recommended.

    You'd usually change it when you want to know which sheet you are using regardless of what others may have changed the (visible) sheet name to.


    Regards, TMS
    Last edited by TMS; 02-06-2013 at 11:56 AM.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Registered User
    Join Date
    02-06-2013
    Location
    Earth
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Dealing with sheets codenames

    Hi,

    I cannot upload the original file to the net, so heres a quick and simplified example of what I'm trying to do:

    UsingCodeName.xlsm

    Assuming I don't know the name/codename of the sheet in advance to write it inside the code, how can I manipulate the current sheet codename to do all sort of actions in the sheet? (pulling a number from a known sheet in the example here)

    Thanks for the help
    Attached Files Attached Files

  5. #5
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,844

    Re: Dealing with sheets codenames

    You can refer to the current sheet that is visible as the "ActiveSheet" regardless of its name or Codename. If you want to run a macro on the visible sheet, just refer to it as the 'ActiveSheet'. If you want to run the same macro on all the sheets, you can use a loop similar to this:
    Please Login or Register  to view this content.
    When you loop through all the sheets, you don't need to know their names because they are accessed using the variable 'ws'. I hope this helps.

  6. #6
    Registered User
    Join Date
    02-06-2013
    Location
    Earth
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Dealing with sheets codenames

    The problam is that I do not necessarily have the sheet visible or active at all times, and some of the functions constantly run in the background with application.Volatile.
    I can also have three or four of these sheets simultanously so I can't risk it using activesheet (except for once to declare this codename perhaps)..
    Last edited by sprites; 02-06-2013 at 05:12 PM.

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,669

    Re: Dealing with sheets codenames

    See: http://www.cpearson.com/excel/codemods.htm

    Note my previous post. It is the norm to set the sheet's codename in the VBE before you use the name. This allows you to access the internal name regardless of what the user has done up front.

    In terms of referring to a copied sheet, it is common practice to copy a sheet and, while the new sheet is still the active sheet, set a variable equal to the new sheet. For example:

    Please Login or Register  to view this content.

    Note that when you copy a sheet, it will be allocated the next available internal number by Excel


    Regards, TMS

  8. #8
    Registered User
    Join Date
    02-06-2013
    Location
    Earth
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Dealing with sheets codenames

    I ended up using some of you tips and just using:

    shtname = ActiveSheet.Name

    and then just refering to

    Sheets(shtname)

    whenever I needed and it works just fine, thanks for the help!

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,669

    Re: Dealing with sheets codenames

    You're welcome. Thanks for the rep.

    You can also use:

    Please Login or Register  to view this content.

    And then refer to ws.


    Regards, TMS

+ 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