+ Reply to Thread
Results 1 to 19 of 19

Refer to Sheets by number

  1. #1
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Refer to Sheets by number

    I have been using the syntax "Sheets(1)" and "Sheets(2)" just in case their worksheet names changes.
    The code snippet below copies values from sheet 1 to sheet 2.
    I noticed in Excel 2016 that when I add a new sheet, it copies values to this new sheet, not the original "Sheets(2)".
    How can it work without specifying the name of the sheet?


    Please Login or Register  to view this content.

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

    Re: Refer to Sheets by number

    When you refer to sheets by number, that number is their order in the workbook.
    What was Sheets(2) can become Sheets(3) if the user moves them.

    It sounds like what you are looking for is to refer to sheets by their code name. This link explains the different ways to refer to a sheet
    https://www.mrexcel.com/forum/excel-...-etcetera.html
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: Refer to Sheets by number

    Thank you.

  4. #4
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: Refer to Sheets by number

    OK, I'm running into problems ... have done research but ... nothing.
    See the code below ... I'm trying to find the last row in Sheet2 (this is the VBA name Sheet2, not the tab name "Sheet2").
    I now understand that Sheets(2) refers to the second sheet in the order laid out in the workbook. It may have nothing to do with VBA name Sheet2.
    So if I insert another sheet, it will refer to the second in order, which may not be VBA Sheet2.
    I realize I can refer to it as Sheets("Sheet2") but the tab name may change ... it has to be the VBA name Sheet2.
    Obviously Sheets(Sheet2) doesn't work.

    Please Login or Register  to view this content.

  5. #5
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Refer to Sheets by number

    When you add sheets just add them at the end:

    Please Login or Register  to view this content.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  6. #6
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: Refer to Sheets by number

    Sorry but I do have to rearrange them.

  7. #7
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Refer to Sheets by number

    Well then you'll need to address them by name

  8. #8
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: Refer to Sheets by number

    Was there no other way?
    Thanks anyway.

  9. #9
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Refer to Sheets by number

    Not if you change the order

  10. #10
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,911

    Re: Refer to Sheets by number

    It’s just:

    Please Login or Register  to view this content.
    Rory

  11. #11
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: Refer to Sheets by number

    RowLast2 = Sheet2.UsedRange.SpecialCells(xlCellTypeLastCell).Row
    No, this is isn't working for me.

  12. #12
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Refer to Sheets by number

    What do you mean by
    this is isn't working for me.
    In Post#4 you say yourself that Sheet2 is the sheet's CodeName ( the one NOT between parenthesis)so why wouldn't that work ?
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  13. #13
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: Refer to Sheets by number

    OK, now I know why.
    I'm calling up the macro from an Add-In.
    It appears there is no way around it, not that I can see???

  14. #14
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Refer to Sheets by number

    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  15. #15
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: Refer to Sheets by number

    Sorry but I don't understand the work in post #14.

  16. #16
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Refer to Sheets by number

    It sets the worksheet variable wks2 to the sheet in the active workbook whose codename is Sheet2.

  17. #17
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: Refer to Sheets by number

    OK thanks I may get this to work.

  18. #18
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: Refer to Sheets by number

    Great it works. Thanks.

  19. #19
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Refer to Sheets by number

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

+ 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] number refer to date
    By allgeef in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-04-2017, 01:39 PM
  2. Refer to Sheets in Formula without INDERECT
    By gerarddevries in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-04-2015, 05:41 AM
  3. Refer to Sheets in formula VBA
    By gerarddevries in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-04-2015, 05:32 AM
  4. [SOLVED] How to refer to more than one sheet with With Sheets
    By mario274 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 12-03-2012, 12:03 PM
  5. Dragging formulas that refer across sheets
    By DPGDPG in forum Excel General
    Replies: 6
    Last Post: 11-23-2009, 04:31 AM
  6. #REF in Sheets that refer to Pivot Tables
    By Will C. in forum Excel General
    Replies: 1
    Last Post: 06-02-2006, 03:54 PM
  7. using a listbox or combobox to refer to columns or sheets
    By fern in forum Excel Programming / VBA / Macros
    Replies: 36
    Last Post: 04-03-2005, 01:42 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