Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
MWE MWE is offline
external usenet poster
 
Posts: 20
Default When DIM statements don't work

I have been using VBA for several years. This may be a
really stupid question, but I have recently encountered a
funny phenomena. I use have always assumed that you
could "stack" variables of the same type in a DIM
statement. For example

Dim A, B, C as Integer
and
Dim A as Integer
Dim B as Intger
Dim C as Integer

would have the same effect, i.e., all variables would be
of type Integer.

I now find that this appears to not be true and that some
variables in a multi-variable Dim statement are considered
Variants.

What is supposed to happen with a multi-variable Dim
statement? If only one of the variables is acutallly
types as I want, are the rest of type Variant? What is
the value of allowing multiple variables if they are not
typed the same?

Thanks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,089
Default When DIM statements don't work

What you have discovered is the case and has been discussed in recent weeks.
The two examples you quote do not have the same effect. Only c is defined
as an integer in the first case.

An extract from the VBA Help file for Dim:

Dim Statement Example
This example shows the Dim statement used to declare variables. It also
shows the Dim statement used to declare arrays. The default lower bound for
array subscripts is 0 and can be overridden at the module level using the
Option Base statement.

' AnyValue and MyValue are declared as Variant by default with values
' set to Empty.
Dim AnyValue, MyValue

' Explicitly declare a variable of type Integer.
Dim Number As Integer

' Multiple declarations on a single line. AnotherVar is of type Variant
' because its type is omitted.
Dim AnotherVar, Choice As Boolean, BirthDate As Date


Regards

Trevor


"MWE" wrote in message
...
I have been using VBA for several years. This may be a
really stupid question, but I have recently encountered a
funny phenomena. I use have always assumed that you
could "stack" variables of the same type in a DIM
statement. For example

Dim A, B, C as Integer
and
Dim A as Integer
Dim B as Intger
Dim C as Integer

would have the same effect, i.e., all variables would be
of type Integer.

I now find that this appears to not be true and that some
variables in a multi-variable Dim statement are considered
Variants.

What is supposed to happen with a multi-variable Dim
statement? If only one of the variables is acutallly
types as I want, are the rest of type Variant? What is
the value of allowing multiple variables if they are not
typed the same?

Thanks



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default When DIM statements don't work

You're mostly right. Variant is the default type for a variable and
that is what you'll get if you don't specify otherwise.

When you do;
Dim a, b, c, d, e As Integer

Only "e" is of type "Integer", the rest are variants.

You could do this:
Dim a As Integer, b As Integer, c As Integer, d, e As Integer

"a" "b" "c" and "e" will be Integers and "d" will be a Variant. - Pikus


---
Message posted from http://www.ExcelForum.com/

  #4   Report Post  
Posted to microsoft.public.excel.programming
No Name
 
Posts: n/a
Default When DIM statements don't work

Many thanks to both Trevor and Pikus for their help. Two
things still bother me:

1. what is the use of a Dim statement if the default type
is what you get anyway?

2. why would the originator of VB not follow the
conventions of other programming languages and allow multi
variables to be typed the same in the same line?


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default When DIM statements don't work

Each programming language has its on syntax and conventions. If they were
all the same, there would be no reason to have more than one language.
Conventions are determined by the author of the language.

A better question is why would someone try to use a programming language
without learning the syntax and conventions.

Use of Dim is not required. So if you want the default type, then you don't
need to use it. However, the usual recommendation is to put in Option
Explict at the top of the module. This forces every variable to be declared
with a dim statement. It is useful because if you mispell you variable, the
debugger will flag it as undeclared. Otherwise, you might miss that you
have created a subtle error.

--
Regards,
Tom Ogilvy

wrote in message
...
Many thanks to both Trevor and Pikus for their help. Two
things still bother me:

1. what is the use of a Dim statement if the default type
is what you get anyway?

2. why would the originator of VB not follow the
conventions of other programming languages and allow multi
variables to be typed the same in the same line?




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
IF statements with * character doesn't work [email protected] Excel Worksheet Functions 5 November 8th 06 03:47 PM
IF Statements (Mutliple Statements) Deezel Excel Worksheet Functions 3 October 19th 06 06:13 AM
Getting multiple IF statements to work Psycho0426 Excel Discussion (Misc queries) 15 August 18th 06 04:31 PM
I'm desperate I posted earlier about "If" statements that don't work but are correct. Now... Marc Excel Worksheet Functions 2 April 5th 06 09:27 PM
Cant get totals to work with IF statements spelingchampeon Excel Worksheet Functions 4 October 9th 05 03:14 PM


All times are GMT +1. The time now is 10:05 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"