Using "Set" variable

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I was looking over the "Set" variable in the help.

It gave the example:

Dim YourObject, MyObject, MyStr
Set MyObject = YourObject ' Assign object reference.
' MyObject and YourObject refer to the same object.
YourObject.Text = "Hello World" ' Initialize property.
MyStr = MyObject.Text ' Returns "Hello World".
' Discontinue association. MyObject no longer refers to YourObject.
Set MyObject = Nothing ' Release the object.

When should you use this? Does it have any advantage over just assigning
MyObject = "Hello World"

Thanks
 
You have to use when referring to an object, e.g.

Set MyObject = Range("A1")

In the case MyObject will be a range variable that points to A1 and so you
can access any of the properties of that object via the range variable, such
as

MyObject.Font.Bold = True

If you are not referring to the object but a property of it that object, you
would not use Set, such as

MyValue = Range("A1").Value

In this case MyValue just contains the value in cell A1, and you do not have
access to an of the other properties of A1.

So, it is horses for courses.

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top