+ Reply to Thread
Results 1 to 8 of 8

When to set a worksheet object?

  1. #1
    Forum Contributor
    Join Date
    03-05-2007
    Location
    Falmouth, VA now, Palm Bay, FL for 2 yrs, was Colorado Springs, CO for ten years; Cedark Park, TX; Zeeland, MI; Wilmette, IL; Princeton Junction, NJ; NY, NY
    MS-Off Ver
    365
    Posts
    583

    When to set a worksheet object?

    Attempting to refine my "Best Practices"...

    Does anyone know:

    1) If using
    Please Login or Register  to view this content.
    then using wsGeorge makes the code more efficient, and,

    2) if so, at what point (how many references to that worksheet) does it become more efficient? (For I doubt if only once it would have any positive effect at all!)?

    Thank-you!

  2. #2
    Registered User
    Join Date
    02-22-2016
    Location
    Harare, Zimbabwe
    MS-Off Ver
    2007 & 2010
    Posts
    71

    Re: When to set a worksheet object?

    That would depend on what you want to do with Worksheets("George").

  3. #3
    Forum Contributor
    Join Date
    09-03-2015
    Location
    IE
    MS-Off Ver
    2003 - 2016
    Posts
    258

    Re: When to set a worksheet object?

    Rambling reply.

    For your simple example, probably not but using the Index number of the worksheet or the Code Name from the VBA properties window would be (ever so slightly) more efficient.

    Where the Object.Object. syntax slows down (processor speed wise) is when walking down the object tree
    Please Login or Register  to view this content.
    VBA can then reference the range directly instead of having to resolve the various objects down to the range.

    Whether or not this makes a noticable difference to the speed of execution is doubtful but if the code is running in a loop then obviously the savings increase with each iteration.

    But setting references like that does help coding - there's less physical typing and and can help to avoid typos if you misspell a variable name (You do use 'Option Explicit', of course?)

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

    Re: When to set a worksheet object?

    It will not be noticeably more efficient unless you are referencing it thousands of times (even then I'm not sure if it's any more efficient). Few VBA developers are doing anything where efficiency matters much.

    What's usually more important is how efficient it makes you. If you can make your code easier to read and easier to change and resistant to bugs, then that will make you more efficient.

    Using a Worksheet object is generally a better practice than hardcoding the worksheet name in several places. (Using the codename for the sheet is even better than referring to the sheet by name because the user can change the name displayed on the tab.) It's also less prone to typos if you use Option Explicit (which you should always use).
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  5. #5
    Registered User
    Join Date
    02-22-2016
    Location
    Harare, Zimbabwe
    MS-Off Ver
    2007 & 2010
    Posts
    71

    Re: When to set a worksheet object?

    Maybe we would be better off trying to help you, if you post the complete Code.

  6. #6
    Forum Contributor
    Join Date
    03-05-2007
    Location
    Falmouth, VA now, Palm Bay, FL for 2 yrs, was Colorado Springs, CO for ten years; Cedark Park, TX; Zeeland, MI; Wilmette, IL; Princeton Junction, NJ; NY, NY
    MS-Off Ver
    365
    Posts
    583

    Re: When to set a worksheet object?

    Thank-you everyone!

    It was one of those conceptual things I was thinking about between cups of coffee this morning - not in any specific instance but in general, aka "Best Practices", and now that I think of it 6StringJazzer, that has come up and bit me with changing tab names. I will start to make the transition!

    Any thoughts on best use of "Sheets()" vs. "Worksheets()" when working with regular worksheets?

  7. #7
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: When to set a worksheet object?

    There's no reason it would matter if you are referring to specific worksheet. But if you are looping and you do something like this:
    Please Login or Register  to view this content.
    then you might run into trouble if you try to refer to a cell when the sheet is a chart sheet, things like that.

  8. #8
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: When to set a worksheet object?

    There's no reason it would matter if you are referring to specific worksheet. But if you are looping and you do something like this:
    Please Login or Register  to view this content.
    then you might run into trouble if you try to refer to a cell when the sheet is a chart sheet, things like that.

+ 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. Replies: 0
    Last Post: 06-19-2014, 02:09 PM
  2. worksheet object as string - loop statement in worksheet index
    By ag273n in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-20-2013, 12:27 PM
  3. Getting the object reference from another worksheet
    By Huugin in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-16-2012, 10:17 AM
  4. Replies: 0
    Last Post: 08-22-2006, 05:45 PM
  5. Replies: 0
    Last Post: 08-22-2006, 05:45 PM
  6. Replies: 0
    Last Post: 08-22-2006, 05:45 PM
  7. Replies: 0
    Last Post: 08-22-2006, 05:45 PM

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