Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 279
Default Why does variant not need set?

I am running VBA 6.5 from Excel 2003.

I have a line like Foo = Range(Cells(1, 1), Cells(1, 2))

If Foo is declared with Dim Foo As Variant, the line runs without error.
If Foo is declared with Dim Foo As Range, I get "Run-time error '91';

Object variable or With block variable not set".

I can fix the assignment by prefixing it with Set to read
Set Foo = Range(Cells(1, 1), Cells(1, 2))

Why does a variant not need Set and a compatible object needs set?

I don't imagine anyone outside Microsoft can give the reason for such
errors not being detected at compilation time. ;)

[I am in the middle of eliminating Variant, where possible in a fairly
large file. I have already applied early binding where possible]
--
Walter Briscoe
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Why does variant not need set?

Walter Briscoe laid this down on his screen :
I am running VBA 6.5 from Excel 2003.

I have a line like Foo = Range(Cells(1, 1), Cells(1, 2))

If Foo is declared with Dim Foo As Variant, the line runs without error.
If Foo is declared with Dim Foo As Range, I get "Run-time error '91';

Object variable or With block variable not set".

I can fix the assignment by prefixing it with Set to read
Set Foo = Range(Cells(1, 1), Cells(1, 2))

Why does a variant not need Set and a compatible object needs set?

I don't imagine anyone outside Microsoft can give the reason for such
errors not being detected at compilation time. ;)

[I am in the middle of eliminating Variant, where possible in a fairly
large file. I have already applied early binding where possible]


Walter,
Refs to objects must always be 'Set'. In your example, the first use of
'Foo' loads the 'contents' of the range, resulting with 'Foo' being a
multi-dim array. Your 2nd use example 'Sets' a ref to a 'range object',
resulting with 'Foo' being a substitute for that object in code.

HTH

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 449
Default Why does variant not need set?

Hi Walter

Afaik a Variant is a pointer, to "anything", where many things are objects
and many will be something different. So Set makes sense maybe half of the
times. No error, a pointer does not need Set, by design.

MS doesn't care much about VB6 since the last millennium celebration and the
sometimes Set sometimes not logic is eliminated in VB.NET If I remember
correct.

HTH. Best wishes Harald

"Walter Briscoe" wrote in message
...
I am running VBA 6.5 from Excel 2003.

I have a line like Foo = Range(Cells(1, 1), Cells(1, 2))

If Foo is declared with Dim Foo As Variant, the line runs without error.
If Foo is declared with Dim Foo As Range, I get "Run-time error '91';

Object variable or With block variable not set".

I can fix the assignment by prefixing it with Set to read
Set Foo = Range(Cells(1, 1), Cells(1, 2))

Why does a variant not need Set and a compatible object needs set?

I don't imagine anyone outside Microsoft can give the reason for such
errors not being detected at compilation time. ;)

[I am in the middle of eliminating Variant, where possible in a fairly
large file. I have already applied early binding where possible]
--
Walter Briscoe


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 420
Default Why does variant not need set?

Just to add...

When you use:
foo = range("A1")
it's the equivalent of:
foo = range("A1").value

Foo is just a plain old value (string, number, ...)

In your case,
foo = range(cells(1,1), cells(1,2))
it's the equivalent of:
foo = range(cells(1,1), cells(1,2)).value

Foo is a 1 row by 2 column array of values.

When/if you use Set, then foo will be a range object (since you're working with
a Range. It'll have all the nice properties of ranges and be able to use all
the methods of ranges, too.


On 09/02/2011 08:09, Walter Briscoe wrote:
I am running VBA 6.5 from Excel 2003.

I have a line like Foo = Range(Cells(1, 1), Cells(1, 2))

If Foo is declared with Dim Foo As Variant, the line runs without error.
If Foo is declared with Dim Foo As Range, I get "Run-time error '91';

Object variable or With block variable not set".

I can fix the assignment by prefixing it with Set to read
Set Foo = Range(Cells(1, 1), Cells(1, 2))

Why does a variant not need Set and a compatible object needs set?

I don't imagine anyone outside Microsoft can give the reason for such
errors not being detected at compilation time. ;)

[I am in the middle of eliminating Variant, where possible in a fairly
large file. I have already applied early binding where possible]


--
Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using created variant value to reference to a variant within my code TD[_3_] Excel Programming 1 April 22nd 09 11:53 AM
Variant CV323 Excel Programming 4 February 16th 07 09:57 PM
Variant/Double?!? PeterArvidsson Excel Programming 6 May 31st 06 04:50 PM
Passing value of variant Andrew Excel Programming 3 May 22nd 06 03:16 AM
Variant owl527[_5_] Excel Programming 2 October 14th 05 09:37 PM


All times are GMT +1. The time now is 01:52 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"