the set statement

chris2727272727

Board Regular
Joined
Jul 10, 2005
Messages
152
Could someone please explain the difference between


myrange = ("a2:a10") and
set myrange = ("a2:a10")
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Well the first one will actually create a string "a2:a10" and the second will cause a compile error, Object required.

Now if you had this.
Code:
myrange = Range("a2:a10")
Then myrange would be a variant array containing the values in A2:A10.

And with this.
Code:
Set myrange = Range("a2:a10")
myrange would be set to an object containing cells A2:A10.

With the second one you would have access to all the properties/methods a range/cell has, not just the value.
 
Upvote 0
Re: Set statemnt

chris2727272727 said:
yeah sort of understand have you got any examples
Examples of what?:)
 
Upvote 0
Well you use the Set statement when you want to create a reference to an object.

You would want to do that if you wanted to say change the properties, eg. background colour, of say a range.

It's actually quite hard to give examples as there are thousands of reasons you might want/need to use Set.

Another example is using it like this.
Code:
Set wbOpen = Workbooks.Open(FileName:="C:\MyExcel.xls")
This creates a reference to the newly opened workbook which you can use in later code to work with and manipulate the workbook.

This also saves on having to select/activate which can slow down code and even cause errors.
 
Upvote 0

Forum statistics

Threads
1,214,593
Messages
6,120,435
Members
448,962
Latest member
Fenes

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top