Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default Question about dim statement

While debugging my code today, I was surprised to find the fix was to
change this dim statement...
Dim lNumber, lValue, lWhatever as Long

to this...
Dim lNumber as Long
Dim lValue as Long
Dim lWhatever as Long

The value would not work correctly unless I separated out the dim
statement. Once I did that, it worked flawlessly and I set it back and
forth to be sure.

I've never encountered that before. I've always been able to place
multiple items in a dim statement. Is Long different? Can someone
enlighten me on this?

Thanks.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Question about dim statement

When you do this:
Dim lNumber, lValue, lWhatever as Long
it's seen as:
Dim lNumber as Variant, lValue as Variant, lWhatever as Long

But I'm not sure what you had to fix to make it work. But I would suspect it
had something to do with the way a Long is initialized (as 0) vs a Variant (as
Empty).

But
if lnumber = 0 then
msgbox "hi"
end if

worked ok for me in xl2003. (It saw that I wanted to compare numbers and Empty
was treated as zero.)

Dim lNumber as long, lValue as long, lWhatever as long

if you wanted just one line



Gregg wrote:

While debugging my code today, I was surprised to find the fix was to
change this dim statement...
Dim lNumber, lValue, lWhatever as Long

to this...
Dim lNumber as Long
Dim lValue as Long
Dim lWhatever as Long

The value would not work correctly unless I separated out the dim
statement. Once I did that, it worked flawlessly and I set it back and
forth to be sure.

I've never encountered that before. I've always been able to place
multiple items in a dim statement. Is Long different? Can someone
enlighten me on this?

Thanks.


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Question about dim statement

Hi Gregg,

Dim lNumber, lValue, lWhatever as Long


the above line does *not* dim each of the three variables as long; INumber
is implicitly dimmed as variant and Ivalue is similarly dimmed as variant.
IWhatever is, howver, explicitly dimmed as long.

This has nothing to do with single or multiline declaration which is
essentially a question of style. So, to explicitly dim the three variables
in a single line declaration, you would write it thus::

Dim lNumber as Long, lValue As Long , lWhatever as Long

Normally, using the implicit default variant may be less efficient but
whether it can engender subtle problems depends on the code you are using.
Personally, I have rarely encountered such a problem. But then again, I
endeavour always to explicitly declare variables.



---
Regards,
Norman



"Gregg" wrote in message
oups.com...
While debugging my code today, I was surprised to find the fix was to
change this dim statement...
Dim lNumber, lValue, lWhatever as Long

to this...
Dim lNumber as Long
Dim lValue as Long
Dim lWhatever as Long

The value would not work correctly unless I separated out the dim
statement. Once I did that, it worked flawlessly and I set it back and
forth to be sure.

I've never encountered that before. I've always been able to place
multiple items in a dim statement. Is Long different? Can someone
enlighten me on this?

Thanks.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default Question about dim statement



Dim x,y as long '=y as long , x as variant.
Dim x,y as string '=y as string, x as variant
Dim x as long,y as long '=correct "combined" syntax.

The errors will start when you assign a variable to another
and 1 is not a variant... Or when you use a function
where either return value or arguments are "typed".


dim s as string, i as integer, l as long, v as variant
'or dim s$, i%, l&, v

v=s 'ok since variant will accept other types
v=i 'ok
v=l 'ok
but
s=v (may give probems depending on the content of v
i=s Will give problems
l=i will give problems...

somae goes for functions/procedure arguments

Function HowLong(s as string) as long
Howlong = len(s)
end function

i=howlong(s) will give a problem as Howlong returns a Long.
l=howlong(v) will also give a problem as Howlong expects a string.

Function HowWide(byval s as string) as long
HowWide=len(s)
end function

l=howwide(v) will now work since the argument s is passed byval
but
i=howwide(v) will NOT work as howwide returns a long.


so.. you could stick to using variants. BUT variant carry a lot of
"fat" and are much slower than using correct types.

also when you use variants for objects you will not have intellisense,
and you may get run time errors that otherwise could be picked up
when compiling.

this stuff is explained in all VB(A) books.
in the first few boring chapters that many people skip cuz they want
to the more exciting stuff....

VBA performance can be very sensitive to correct data typing.
(you may not notice when you run a function 1x.. but you WILL notice
when a function is called 100000 times..)


--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


Gregg wrote :

While debugging my code today, I was surprised to find the fix was to
change this dim statement...
Dim lNumber, lValue, lWhatever as Long

to this...
Dim lNumber as Long
Dim lValue as Long
Dim lWhatever as Long

The value would not work correctly unless I separated out the dim
statement. Once I did that, it worked flawlessly and I set it back and
forth to be sure.

I've never encountered that before. I've always been able to place
multiple items in a dim statement. Is Long different? Can someone
enlighten me on this?

Thanks.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default Question about dim statement

keepITcool wrote:
The errors will start when you assign a variable to another
and 1 is not a variant... Or when you use a function
where either return value or arguments are "typed".


this stuff is explained in all VB(A) books.
in the first few boring chapters that many people skip cuz they want
to the more exciting stuff....


I totally misread that part in the chapter! Looking back at my notes
from when I first learned multiple line dim statements, it specifically
warns NOT to dim as I did. <jeesh Somehow, I misread and did exactly
that.

Oddly enough, since using that incorrect method for about a year now, I
have not ran into trouble until now. Lucky, I guess.

I'll be sure, however, to go back on all my programming and change the
dim statements to the correct method.

Thank you, Dave, Norman, and KeepItCool for your time and patience in
explaining this.

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
SQL statement question gab1972 Excel Discussion (Misc queries) 2 November 19th 09 09:42 PM
IF Statement question peg84 Excel Discussion (Misc queries) 3 February 4th 08 10:19 PM
If statement question ranvin56 Excel Discussion (Misc queries) 2 December 12th 07 12:41 AM
If Statement Question redwings04 Excel Worksheet Functions 4 July 4th 05 09:42 PM
If Statement Question carl Excel Worksheet Functions 1 March 11th 05 04:55 AM


All times are GMT +1. The time now is 12:28 AM.

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"