+ Reply to Thread
Results 1 to 5 of 5

how do i reference a worksheet variable in a formula?

Hybrid View

  1. #1
    Registered User
    Join Date
    07-03-2012
    Location
    Dubai
    MS-Off Ver
    Excel 2010
    Posts
    23

    how do i reference a worksheet variable in a formula?

    Hey!

    I some code similar to this:

    Formula: copy to clipboard
    Sub lastdatapoint()
    Dim lastdatapoint As Long
    Dim SheetName As Worksheet

    Set SheetName = Worksheets("FB1")
    lastdatapoint = Worksheets("SheetName").Cells(Rows.Count, 2).End(xlUp).Row

    End Sub


    And I get a "subscript out of range error" for this small excerpt, and with my other code, it doesn't seem to recognize the reference of lastdatapoint = Worksheets("SheetName"). So I'm wondering if my use of quotation marks is wrong? or maybe something else?

    Any suggestions are VERY welcome!

    Thanks in advance,

    Dan

  2. #2
    Registered User
    Join Date
    07-03-2012
    Location
    Dubai
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: how do i reference a worksheet variable in a formula?

    I should mention that SheetName does get set to FB1. I know this because if I go insert the code: SheetName.Activate , then the FB1 worksheet comes up

  3. #3
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: how do i reference a worksheet variable in a formula?

    in spite of its name SheetName is a worksheet object, not the name of one so you need:
    lastdatapoint = SheetName.Cells(SheetName.Rows.Count, 2).End(xlUp).Row
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  4. #4
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: how do i reference a worksheet variable in a formula?

    lastdatapoint = SheetName.Cells(Rows.Count, 2).End(xlUp).Row

  5. #5
    Registered User
    Join Date
    07-03-2012
    Location
    Dubai
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: how do i reference a worksheet variable in a formula?

    Many thanks to both of you! Especially for the quick responses

+ 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