+ Reply to Thread
Results 1 to 5 of 5

when to use the keyword "set"

  1. #1
    Registered User
    Join Date
    07-21-2009
    Location
    Richmond, VA
    MS-Off Ver
    Excel 2019
    Posts
    13

    Smile when to use the keyword "set"

    When do we use the keyword "set" in VBA? Try search in the threads, no result.

    One of the examples(Walkenbach excel 2003 vba book), below:

    Set rng = intersect(rng,parent.usedrange, rng)

    How do i know when to use the keyword "set"?

    Thanks in advance.

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: when to use the keyword "set"

    Nick Hodge has a related article which may be of interest: http://www.nickhodge.co.uk/pre2007/vba.htm

    (more specifically see section re: Objects)

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

    Re: when to use the keyword "set"

    There are two flavors of assignments in VBA: one for ordinary variables, which use Let, and one for object variables, which use Set.

    An ordinary (string, logical, numeric) variable is one that points to the location in memory where the variable is stored.

    An object variable (all the things you find in the language reference under Objects) is one that points to a structure in memory (a VTable) that in turn contains pointers to the object's properties and methods.

    Please Login or Register  to view this content.
    The Let in VBA is implicit and seldom seen.

    EDIT: Arrays in VBA are 'ordinary' in the context above; an array in VBA is the memory address of a structure called a Safe Array that contains the data type, lower bounds, and array dimensions.
    Last edited by shg; 07-30-2009 at 07:34 PM.
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Registered User
    Join Date
    07-21-2009
    Location
    Richmond, VA
    MS-Off Ver
    Excel 2019
    Posts
    13

    Smile Re: when to use the keyword "set"

    Thank you for answering my question. I have another question pertaining to your answer.

    Here is how i interpret your statement (An ordinary (string, logical, numeric) variable is one that points to the location in memory where the variable is stored.) :

    Using the example you have given the "a" variable contains the address of the variable that stores the results of b+c?

    Thanks in advance.

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

    Re: when to use the keyword "set"

    It contains the address of a, whatever it stores. The compiler knows the data type (and therefore its length and storage format) based on its declaration. If undeclared (bad practice), it's a Variant, which contains information about its subtype (string, numeric, object, ...). See the VarType function.
    Last edited by shg; 07-30-2009 at 07:37 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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