+ Reply to Thread
Results 1 to 14 of 14

object variable question

  1. #1
    Forum Contributor
    Join Date
    11-05-2006
    Posts
    123

    object variable question

    I am using the following code. It looks at a cell's value, then changes the color of another cell on the same row based on that value:

    Please Login or Register  to view this content.
    I used the object variable "cell" simply because it makes sense. But I realize I could have used "ce" or any something else because it is simply a variable taking on the properties of an object.

    What I don't understand is how does the expression know which object I am referring to? Another example would be
    Please Login or Register  to view this content.

    I just don't understand how the code understands which object I am referring to. It seems as if the object is implied by context. But I am new to this and trying to make sense of it.
    Last edited by Wedge120; 11-27-2010 at 01:42 PM.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: object variable question

    You should declare your variables
    eg
    Please Login or Register  to view this content.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Forum Contributor
    Join Date
    11-05-2006
    Posts
    123

    Re: object variable question

    Quote Originally Posted by royUK View Post
    You should declare your variables
    eg
    Please Login or Register  to view this content.

    Based on what I have read, I agree, because it makes sense and can speed up the code (but I don't have experience in coding, so I am going by what I have read). But that still doesn't really tell me how the expression knows which object it points to.

    Not trying to be nit-picky because I appreciate the help, but I really want to know.

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: object variable question

    Your code is simply looping through the objects, by declaring the variables correctly excel knows that it is looking for a Range object or a WorkSheet. See this

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: object variable question

    What I don't understand is how does the expression know which object I am referring to?
    You are iterating through a collection, in this case a Range.

    If you don't declare cell (or whatever you call it; it doesn't matter, but for the fact that you should adopt a variable naming convention and stick to it) as a Range, then you are iterating through the default collection of a range, which is Cells. To make it explicit (and you should),

    Please Login or Register  to view this content.
    An range has other collections, including Rows and Columns.

    See Help for Returning an Object from a Collection
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Forum Contributor
    Join Date
    11-05-2006
    Posts
    123

    Re: object variable question

    Quote Originally Posted by shg View Post
    You are iterating through a collection, in this case a Range.

    If you don't declare cell (or whatever you call it; it doesn't matter, but for the fact that you should adopt a variable naming convention and stick to it) as a Range, then you are iterating through the default collection of a range, which is Cells.
    I think that's what I was wanting to know.



    Quote Originally Posted by shg View Post
    To make it explicit (and you should),

    Please Login or Register  to view this content.
    An range has other collections, including Rows and Columns.

    See Help for Returning an Object from a Collection
    Much appreciated.

  7. #7
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: object variable question

    In this case I don't think specifying or declaring serves any purpose:
    You can't declare better than the VBA-compiler can, so leave the 'thinking' to computer.
    See:

    Please Login or Register  to view this content.



  8. #8
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: object variable question

    One of the greatest Excel Guru's opinion

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: object variable question

    In this case I don't think specifying or declaring serves any purpose:
    You can't declare better than the VBA-compiler can, so leave the 'thinking' to computer.
    I'll go on record saying that is a lousy suggestion shared by vanishingly few competent programmers.

    And all those typenames uninformatively return "Range"
    Last edited by shg; 11-27-2010 at 11:13 PM.

  10. #10
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: object variable question

    And all those typenames uninformatively return "Range"
    What is so uninformative about the dimension those variables have ? (what declaring is all about)
    Do you suggest this is possible ?

    Please Login or Register  to view this content.
    I think your comment underlines my proposition you can't do better than the VBA-compiler.

  11. #11
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: object variable question

    If Variables aren't declared explicitly then Excel assigns them as a Variant. This can cause longer processing times.

    If Variables aren't declared properly & Option Explicit used then typos will lead to unwanted variables being created, & possible make later code not run as expected.

    Declaring Variables properly makes for faster code & easier maintenance

  12. #12
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: object variable question

    Please Login or Register  to view this content.
    Quod non, as I showed you with
    Please Login or Register  to view this content.

  13. #13
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: object variable question

    Quod non ...
    But it is. Look in the Locals window to see that it is a variant/object/range

  14. #14
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,016

    Re: object variable question

    Quote Originally Posted by snb View Post
    In this case I don't think specifying or declaring serves any purpose
    Since you've previously stated that you think declaring variables serves no purpose, why do you say "in this case"?
    Everyone who confuses correlation and causation ends up dead.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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