Consulting

Results 1 to 10 of 10

Thread: Sheet1(MyTab)

  1. #1

    Sheet1(MyTab)

    Hi guys and ladies,

    In the VBE when you look at the worksheet names on the left pane. There is the Sheet Number followed by the tab name. --IE:Sheet1(MyTab)-- You can, in a macro use either of these to refer to a worksheet.
    So if i have a macro where i want a reference to not change even if the tab is renamed i want to use the "Sheet1" reference.

    Worksheets("Sheet1").Select
    Worksheets("MyTab").Select
    In these two examples the first doesn't work. The second does. Would you explain a little about this? (why, how to, etc)

    Thx
    Dave
    "The game is afoot Watson"

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    The Code Name can be used, but there are no quotes and you don't use Worksheets or Sheets to refer to it.

    So for example, just use this.


    Sheet1.Select

  3. #3
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location
    Quote Originally Posted by Piranha
    Hi guys and ladies,

    In the VBE when you look at the worksheet names on the left pane. There is the Sheet Number followed by the tab name. --IE:Sheet1(MyTab)-- You can, in a macro use either of these to refer to a worksheet.
    So if i have a macro where i want a reference to not change even if the tab is renamed i want to use the "Sheet1" reference.

    Worksheets("Sheet1").Select
    Worksheets("MyTab").Select
    In these two examples the first doesn't work. The second does. Would you explain a little about this? (why, how to, etc)

    Thx
    I do not think that you can reference a sheet by its "name" in the VBE. You can reference a sheet using either its index or its name. The index is simply the # assigned to it based on the order of tabs, left to right. So the leftmost tab is 1, the next is 2, etc. Thus if the leftmost tab is named "MyTab" then you could reference it in two ways:

    Worksheets(1).Select
     Worksheets("MyTab").Select
    The indices for sheets will change if you reorder the sheets (tabs). It is not wise to track or refer to a sheet by its index unless you really do not care which sheet it is. For example
    For I = 1 to Worksheets.Count
    Msgbox Worksheet(I).Name
    Next I
    would display the name of each sheet. The order is whatever the order of the tabs is. The order of sheets displayed using

    Dim wk as Worksheet
    For each wk in Worksheets
    Msgbox wk.Name
    Next wk
    is the same, i.e., the order of the tabs.

    Part of the confusion stems from the fact that initially the true name of the sheet (as viewed by the user in the lower tab and the name by which the sheet is known in VBA) and the "name" of the sheet in the VBE are the same, e.g., "Sheet1".

    The "name" for a sheet in the VBE (your "Sheet1" example) can be changed. Display the properties window in the VBE and click on the relevant sheet. The Name property can then be changed.

    Confusing?

    EDIT: and then DRJ says you CAN refer to a sheet by its "VBE name" and I feel like a jerk !
    "It's not just the due date that's important, it's also the do date" [MWE]

    When your problem has been resolved, mark the thread SOLVED by clicking on the Thread Tools dropdown menu at the top of the thread.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by MWE
    I do not think that you can reference a sheet by its "name" in the VBE.
    That is its codename and is exposed to VBA so is useable.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    To clarify, or muddy the waters (delete whichever is not applicable) ...

    The CodeName is an object reference - use it just as if you had declared it something like this:

    Set Sheet1CodeName = Sheets("Sheet1TabName")

    It can be changed as MWE says via the VBE, but not by code so it gives a guaranteed reference to the sheet in question. It's the equivalent, for a sheet, to ThisWorkbook (which can also be renamed via the VBE properties box) for a workbook.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  6. #6
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Hi Piranha,

    Perhaps this'll help you? > http://www.vbaexpress.com/forum/arti...ticle&artid=35

    HTH,
    John
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by TonyJollans
    It can be changed as MWE says via the VBE, but not by code so it gives a guaranteed reference to the sheet in question.


    ThisWorkbook.VBProject.VBComponents("Sheet3").Properties("_CodeName").Value = "NewCodeName"
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    It's a good idea to use the code name, however make it something relevant!

    shRawData.select
    rather than

    Sheet1.Select

  9. #9
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Quote Originally Posted by xld
    [VBA]
    ThisWorkbook.VBProject.VBComponents("Sheet3").Properties("_CodeName").Value = "NewCodeName"
    [/VBA]
    Alright, it can be done I had forgotten that. Thanks.

    I'm not about to explain how that affects usage, and in which procedures. I'll let Piranha come to that in due course.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  10. #10
    Quote Originally Posted by johnske
    Hi Piranha,

    Perhaps this'll help you? > http://www.vbaexpress.com/forum/arti...ticle&artid=35

    HTH,
    John
    Hi, DRJ, MWE, xld,TonyJollans, geekgirkau, johnske,
    Thank you all for you input. I got it now. You all are great.

    johnske - Great link - thx
    Dave
    "The game is afoot Watson"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •