+ Reply to Thread
Results 1 to 6 of 6

How to Assign Variable Name to Worksheet Object Using Its Code Name

  1. #1
    Forum Contributor
    Join Date
    03-26-2015
    Location
    Cookeville, TN
    MS-Off Ver
    Excel 365
    Posts
    202

    How to Assign Variable Name to Worksheet Object Using Its Code Name

    Hi, newbie here...

    I see in various tutorials that worksheets can be referred to by number, or name, or code name. Suppose I have a worksheet named "Data" whose code name is Sheet3. I'd like to assign a variable name (let's say "MySheet") to this worksheet, so that I can simply use MySheet in my macro instead of typing ThisWorkbook.Worksheets("Data"). But I would also like my macro to always refer to that sheet (even if the user moves it or renames it), so I'd like to define MySheet using the code name rather than the worksheet name (Data). Unfortunately, when I use code like this:
    Please Login or Register  to view this content.
    I get a compile error (Method or data member not found)! How can Sheet3 be "not found" when it's right there in the workbook? If I use this:
    Please Login or Register  to view this content.
    it works fine. But I want to use the code name rather than the worksheet name! What am I doing wrong?? Don't Worksheets("Data") and Sheet3 both refer to the same object?!?

  2. #2
    Registered User
    Join Date
    02-27-2015
    Location
    West
    MS-Off Ver
    2010 Professional
    Posts
    18

    Re: How to Assign Variable Name to Worksheet Object Using Its Code Name

    Try this
    Please Login or Register  to view this content.

  3. #3
    Forum Contributor
    Join Date
    03-26-2015
    Location
    Cookeville, TN
    MS-Off Ver
    Excel 365
    Posts
    202

    Re: How to Assign Variable Name to Worksheet Object Using Its Code Name

    Quote Originally Posted by vlookup. View Post
    Try this
    Please Login or Register  to view this content.
    But doesn't that refer to the second worksheet (in the current order)? So if the user moves one of the worksheets, the macro could then refer to the incorrect sheet, right? I don't think the sheet number, and the number in the code name, refer to the same thing.

  4. #4
    Registered User
    Join Date
    02-27-2015
    Location
    West
    MS-Off Ver
    2010 Professional
    Posts
    18

    Re: How to Assign Variable Name to Worksheet Object Using Its Code Name

    It appears that you're correct. Sorry.

    The following worked for me, even when I changed the order of the sheets:
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    03-26-2015
    Location
    Cookeville, TN
    MS-Off Ver
    Excel 365
    Posts
    202

    Re: How to Assign Variable Name to Worksheet Object Using Its Code Name

    OK, thanks, that's a step in the right direction! Now how do I ensure that (if there are multiple workbooks open) Sheet2 always refers to the Sheet2 that's in "ThisWorkbook"? Is that the default if the specific workbook isn't referenced?

  6. #6
    Registered User
    Join Date
    02-27-2015
    Location
    West
    MS-Off Ver
    2010 Professional
    Posts
    18

    Re: How to Assign Variable Name to Worksheet Object Using Its Code Name

    I assume so. If it required that the workbook be specified, then this code would return an error.

+ 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] Excel VBA error code 91: Object variable or with block variable not set
    By wilnexpc in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-24-2013, 11:46 AM
  2. Assign a variable to an object parameter
    By gm13958 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 01-07-2011, 12:26 PM
  3. assign objects into array or object variable?
    By Fendic in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-22-2005, 05:05 PM
  4. Add new worksheet and assign it to an object variable?
    By Thief_ in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-04-2005, 03:06 AM
  5. FileSearch Object - Cannot assign to variable
    By Alan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-24-2005, 12:06 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