Results 1 to 10 of 10
  1. #1
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Benefits of creating an object (VBAExcel)

    Good day all
    I note that the more experienced practitioners will create an object such as oMyWksht and then after setting it equal to WorksheetA, interrogate oMyWksht instead of interrogating WorksheetA.
    The advantages of this approach escape me. Any guidance will be appreciated.
    Regards
    Don

  2. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Benefits of creating an object (VBAExcel)

    Using an object variable improves the code performance because you don't have to keep rereferencing the object itself. Otherwise, VBA has to validate each reference back to WorksheetA and this slows things down. When you instantiate an object reference (Set MyObject = ObjectA), the variable holds on to the valid reference, so the engine doesn't need to do any extra work on it.
    Charlotte

  3. #3
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Benefits of creating an object (VBAExcel)

    Thank you Charlotte.
    Regards
    Don

  4. #4
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Benefits of creating an object (VBAExcel)

    Don,

    Another practical benefit is that by declaring the worksheet object, you then have access to intellisense when developing the code.

    For example, if you type "Worksheets("Sheet1")" and then type a period, you won't get the automatic list of class members.

    But if you use:
    Dim oWorksheet As Worksheet
    Set oWorksheet = Worksheets("Sheet1")
    oWorksheet

    - if you then type a period after "oWorksheet", the intellisense will kick in. Someone more versed in Excel may be able to explain better, but if I understand it correctly, the reason intellisense doesn't work in the first example, but does in the second, is that there are a number of worksheet types in Excel, each of which has its own specific list of class members.

    The first code example references the generic Worksheets object; listing the members isn't possible because the worksheet Type isn't specified. Although I'm not sure of the exact mechanism, when you set an object reference to a specific worksheet, somehow the information about the Type of that worksheet, is included in the new object's properties, therefore the list of members for that type of worksheet can be displayed.

    Gary

  5. #5
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Benefits of creating an object (VBAExcel)

    Thank you Gary
    That phenomenon has been bothering me for some time.
    Regards
    Don

  6. #6
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Benefits of creating an object (VBAExcel)

    One other benefit. oSh1 is a lot less typing than Worksheets("Sheet1").
    Legare Coleman

  7. #7
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Benefits of creating an object (VBAExcel)

    As far as I'm concerned this is just a bug in the VBE. There are different SHEETS, bot there is only one WORKSHEET object.
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  8. #8
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Benefits of creating an object (VBAExcel)

    As Martha Stewart would say, "And that's a good thing."
    Regards
    Don

  9. #9
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Re: Benefits of creating an object (VBAExcel)

    I'm "piling on" to good answers already provided, but Chip Pearson has some advice here and here which you may find useful, Don.
    -John ... I float in liquid gardens
    UTC -7ąDS

  10. #10
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Benefits of creating an object (VBAExcel)

    Jan Karel,

    Thanks for clarifying. That certainly is a strategically inconvenient bug! - the lack of intellisense for that central object, makes Excel VBA more user-unfriendly than it needs to be, at least until you learn to set that object reference.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •