• Benefits of creating an object (VBAExcel)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Benefits of creating an object (VBAExcel)

    Author
    Topic
    #416959

    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.

    Viewing 0 reply threads
    Author
    Replies
    • #934074

      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.

      • #934087

        Thank you Charlotte.

        • #934311

          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

          • #934312

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

            • #934389

              One other benefit. oSh1 is a lot less typing than Worksheets(“Sheet1”).

            • #934399

              As Martha Stewart would say, “And that’s a good thing.”

            • #934400

              I’m “piling on” to good answers already provided, but Chip Pearson has some advice here and here which you may find useful, Don.

          • #934392

            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.

            • #934421

              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

    Viewing 0 reply threads
    Reply To: Benefits of creating an object (VBAExcel)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: