+ Reply to Thread
Results 1 to 5 of 5

Easy question about Objects in VBA for Excel

  1. #1
    Registered User
    Join Date
    02-02-2014
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    5

    Easy question about Objects in VBA for Excel

    Hi all,

    I have a small question... I see that Application.ActiveCell and Application.ActiveWindow.ActiveCell both point to the same thing. How come??? Can you skip any number of objects from the object hierarchy like this? So , 1.2.3.4 or 1.4 are same?? Is this applicable only for Excel VBA or OOP in general?

    -SM.
    Last edited by saurabhmangal; 02-02-2014 at 08:59 AM. Reason: clarity of question

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,995

    Re: Easy question about Objects in VBA for Excel

    This is specific to VBA, not any other OOPL. In VBA, the objects in the hierarchy as in your example are called "qualifications." In VBA, if you leave out the qualification, VBA has rules about what it will use as the default.

    In the example you gave, you did not "skip" objects in the hierarchy, even though it looks that way. ActiveCell defaults to Application.ActiveCell. ActiveWindow is a different type of object that also has cells, and it just so happens that ActiveWindow also has an active cell, and by their definitions they will be the same. Technically the two don't mean the same thing, but they end up pointing to the same object. It's like giving someone a street address vs. saying, "Go to the second house on the left." They both get you to the same place but they mean something different.

    Here's another example. If you use the expression

    Please Login or Register  to view this content.
    you have omitted the qualification for the Worksheet. It can have two different defaults, depending on where the code is. If the code is in a module for a Sheet object, it will default to that sheet.

    Please Login or Register  to view this content.
    If it is in a Module, then it will default to ActiveWorksheet.

    Please Login or Register  to view this content.
    To be able to "skip" qualifications, you have to really understand what each qualification does and what the defaults are. That is why it is a best practice to make all qualifications explicit. If you give your code to someone else to maintain, or even if you look at your own code 6 months after you write it, it may be unclear what was intended, and this is a real nightmare if there is a bug.
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    02-02-2014
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Easy question about Objects in VBA for Excel

    Thanks for the wonderful explanation... My follow-up questions:

    1) How many moderators are there for VBA / MAcros section of the site/forum? Are all active like you
    2) How do I learn about such qualifications/defaults in VBA?
    3) Can this skipping be in other OOPL e.g.
    Please Login or Register  to view this content.
    or saying directly by skipping "Wheel" as
    Please Login or Register  to view this content.
    . Is this related to inheritance where the same property is there between hierarchically different objects?

    Thanks again.

    -SM

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

    Re: Easy question about Objects in VBA for Excel

    1) How many moderators are there for VBA / MAcros section of the site/forum? Are all active like you

    I don't know how many. Moderators are not assigned to any particular forum; each Moderator has freedom to go wherever he/she wants or feels needed. As a member, I answer questions (and also ask some!), just like other members who are Experts, Gurus, or other categories. My role as a Moderator is to ensure people follow rules, so Moderators are

    2) How do I learn about such qualifications/defaults in VBA?

    There are lots of resources on the web, and textbooks. I would suggest starting with a search on "Excel VBA object model." Once you understand the object model, the syntax becomes much more intuitive.

    3) Can this skipping be in other OOPL e.g.
    Please Login or Register  to view this content.
    or saying directly by skipping "Wheel" as
    Please Login or Register  to view this content.
    . Is this related to inheritance where the same property is there between hierarchically different objects?

    I don't know of an OOPL where you can omit qualifications like that. I have used C++, Ada, and Java, and have some minimal exposure to Smalltalk. It is completely unrelated to inheritance.

    Hope that helps,

  5. #5
    Registered User
    Join Date
    02-02-2014
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Easy question about Objects in VBA for Excel

    Thanks all

+ 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] organised data with multiple critera in the same column
    By yzbob in forum Excel - New Users/Basics
    Replies: 4
    Last Post: 02-05-2013, 01:14 AM
  2. Easy Excel Question
    By czah7 in forum Excel General
    Replies: 3
    Last Post: 11-15-2011, 03:01 AM
  3. easy excel question
    By ness80 in forum Excel General
    Replies: 5
    Last Post: 01-22-2007, 02:05 PM
  4. Easy Excel VB question (I think!)
    By Just Kev in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-02-2006, 10:00 AM
  5. Question - How to group unique objects in Excel
    By bradsaxon in forum Excel General
    Replies: 1
    Last Post: 04-14-2006, 12:55 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