+ Reply to Thread
Results 1 to 6 of 6

Referencing the first sheet, no matter its name.

Hybrid View

  1. #1
    Registered User
    Join Date
    02-04-2011
    Location
    boulder, co
    MS-Off Ver
    Excel 2003
    Posts
    74

    Referencing the first sheet, no matter its name.

    Hi,

    Is there a way that you can refer to the first sheet in a workbook, without knowing its name?

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    12,981

    Re: Referencing the first sheet, no matter its name.

    Sheet1.Range( ...
    Sheets(1). ...
    Ben Van Johnson

  3. #3
    Registered User
    Join Date
    02-04-2011
    Location
    boulder, co
    MS-Off Ver
    Excel 2003
    Posts
    74

    Re: Referencing the first sheet, no matter its name.

    Hrm. It turned my #REF error into a #value error. Just for reference, I'm working with this equation:
    =INDIRECT.EXT("'N:\QB SKU Project Backup\NON-PROTECTED\["&$B$43&"]COST SHEET'!$"&CHAR(63+COLUMN(B23))&MOD(ROW(B23),10)+7&"")
    Which turns into this given your recommendation:
    =INDIRECT.EXT("'N:\QB SKU Project Backup\NON-PROTECTED\["&$B$43&"]"&sheets(1)&"'!$"&CHAR(63+COLUMN(B23))&MOD(ROW(B23),10)+7&"")
    I've tried this with different files which give me the correct result using the first equation, but will turn into #value error when inputting the "&sheets(1)&"

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    27,066

    Re: Referencing the first sheet, no matter its name.

    The code from protonLeah is how you would reference the first sheet in a VBA macro, not an Excel formula.

    In formulas, you can refer to a sheet by name, but not by its position. You would need to write a UDF like this:

    Public Function Sheet(i As Long, s As String) As Range
    
       Set Sheet = Worksheets(i).Range(s)
    
    End Function
    This will take a sheet index and a string giving a range reference, and return the reference.

    This will return the contents of cell A1 on the first sheet, whatever that is:

    =Sheet(1, "A1")

    The function call can be used anywhere a range can be used:

    =VLOOKUP(A1,Sheet(1,"A1:B5"),2,FALSE)
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    27,066

    Re: Referencing the first sheet, no matter its name.

    Because you are building a string and using INDIRECT, you could also use a function like this that returns a sheet name:

    Public Function SheetName(i As Long) As String
       Application.Volatile ' force updates
       SheetName = Worksheets(i).Name
    End Function
    BTW because these functions do not refer to a particular range, you would also have to force them to be updated any time there's a change. However, changing the order of worksheets would not force the update. This gets a little tricky and I haven't taken the time to think through the details, but this gives you the idea of what would be involved in something like that.

  6. #6
    Registered User
    Join Date
    02-04-2011
    Location
    boulder, co
    MS-Off Ver
    Excel 2003
    Posts
    74

    Re: Referencing the first sheet, no matter its name.

    Hmm. So I couldn't get the Sheet() function to work, but that was ok, because the sheetname() proved to be a better fit. However, while I can get it to work in a standalone scenario, giving me the sheet names of the current workbook, adding it to my indirect function I believe it's trying to pull the current workbook sheet names in lieu of the referenced workbook sheet names.(ewww, a run-on sentence). I don't know UDF or VBA coding at all, but I believe there needs to be a file reference parameter within the sheetname() function. Although if at all possible I'd like to avoid that, as having the filepath and name twice in the same equation seems lengthy and ridiculous.

    Please help!

+ 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