A Whole Lot a Dimming Going On

R

Robert Lerner

I apologize up front if this is a really dumb question.

My procedure [1 module/1 sub/900 lines (no comments)] involves 57 dim
statements.

Lots of variables, but it runs great. It just looks strange with all the Dim
statements.

Is that too many? Is it bad coding practice? Any tips to do handle it
better?

Thank you,
Robert
 
C

CoRrRan

I apologize up front if this is a really dumb question.

My procedure [1 module/1 sub/900 lines (no comments)] involves 57
dim statements.

Lots of variables, but it runs great. It just looks strange with
all the Dim statements.

Is that too many? Is it bad coding practice? Any tips to do handle
it better?

Thank you,
Robert

Robert,

You can ignore using the DIM-statement for each variable in the
following manner:

Dim strString1 as String, strString2 as String, _
strString3 as String, strString4 as String
Dim iInteger1 as Integer, iInteger2 as Integer, iInteger3 as Integer
Dim sngSingle1 as Single, sngSingle2 as Single
Dim dblDouble1 as Double, dblDouble2 as Double

This way you can use one DIM statement to declare a number of
variables in a single line of code.

Do not use the following way, as it will provide you with a number of
unwanted Variant-datatypes:

Dim strString1, strString2, strString3 as String

In the above line, only the last variable (string 3) will have the
datatype "String", the other two are of datatype "Variant". This is
hardcoded into VBA, and even in VB, I believe.

Sometimes it is usefull to use a DIM-statement for a single variable,
as this gives you some space on the right side of the code to insert
a comment for the specific variable. However, if you do not need
comments, then it is much easier to use the first example I gave.

You can also intermix datatypes in a single line in the following
manner, but I do not find that handy myself:

Dim strString1 as String, dblDouble1 as Double, iInteger1 as Integer

HTH,

CoRrRan

P.S. I use the "Hungarian"-notation (perhaps a small derivative) for
coding variables. Take a look at: http://tinyurl.com/wmwg (Link to
MS-support)
 
J

JE McGimpsey

57 variables in 900 lines sounds like a lot, but without seeing your
code, it's hard to say whether it's too many.

If the code is for your use only, my philosophy is usually "if it runs
the way you want, it's correct".

If the code will need to be maintained by others, you're better off
using a more formal system.

One shortcut, which I usually don't recommend, but can save a lot of
Dim'g is to use a DefType statement. For instance, at the module level,
putting

DefStr S

will automatically type all variables starting with "s" or "S" as
strings, so

Dim sOne, sTwo, sThree, sFour

is equivalent to

Dim sOne As String
Dim sTwo As String
Dim sThree As String
Dim sFour As String

or

Dim sOne As String, sTwo As String, sThree As String, sFour As String
 
C

CoRrRan

57 variables in 900 lines sounds like a lot, but without seeing
your code, it's hard to say whether it's too many.

If the code is for your use only, my philosophy is usually "if it
runs the way you want, it's correct".

If the code will need to be maintained by others, you're better
off using a more formal system.

One shortcut, which I usually don't recommend, but can save a lot
of Dim'g is to use a DefType statement. For instance, at the
module level, putting

DefStr S

will automatically type all variables starting with "s" or "S" as
strings, so

Dim sOne, sTwo, sThree, sFour

is equivalent to

Dim sOne As String
Dim sTwo As String
Dim sThree As String
Dim sFour As String

or

Dim sOne As String, sTwo As String, sThree As String, sFour As
String


My procedure [1 module/1 sub/900 lines (no comments)] involves 57
dim statements.

Lots of variables, but it runs great. It just looks strange with
all the Dim statements.

Is that too many? Is it bad coding practice? Any tips to do
handle it better?

Mr McGimpsey,

Can you tell me why you wouldn't 'recommend' the use of the DefType-
statement? I didn't know that this statement existed and I think it
might be somewhat useful. I'll have to make sure that my code is
correct and that I don't accidentaly dimension a variable with the
wrong starting letter, but otherwise it might be quite useful.

CoRrRan
 
J

JE McGimpsey

Personal preference, mostly. If one isn't using a strict variable naming
convention, it's easy when using DefType to accidently mistype a
variable.

If one is using a strict variable naming convention, then it *may* be
useful, unless more than one type uses the same initial letter (such as
strString, sngSingle, or dblDouble, datDate). When using scope prefixes,
such as "g" for global, it's useless.

Since the DefType statements are at the module level, it also makes it
somewhat more difficult to maintain individual procedures - or to move
them to new modules.

Also, I tend to break my code into small procedures, rarely more than
100 lines, and rarely more than 6 or 7 variables per procedure, so the
efficiency of using DefType is marginal.
 
N

Nick Burns

Is there listing of Hungarian notation for Excel specfic objects:
Workbook
WorkSheet
Range
etc.

CoRrRan said:
I apologize up front if this is a really dumb question.

My procedure [1 module/1 sub/900 lines (no comments)] involves 57
dim statements.

Lots of variables, but it runs great. It just looks strange with
all the Dim statements.

Is that too many? Is it bad coding practice? Any tips to do handle
it better?

Thank you,
Robert

Robert,

You can ignore using the DIM-statement for each variable in the
following manner:

Dim strString1 as String, strString2 as String, _
strString3 as String, strString4 as String
Dim iInteger1 as Integer, iInteger2 as Integer, iInteger3 as Integer
Dim sngSingle1 as Single, sngSingle2 as Single
Dim dblDouble1 as Double, dblDouble2 as Double

This way you can use one DIM statement to declare a number of
variables in a single line of code.

Do not use the following way, as it will provide you with a number of
unwanted Variant-datatypes:

Dim strString1, strString2, strString3 as String

In the above line, only the last variable (string 3) will have the
datatype "String", the other two are of datatype "Variant". This is
hardcoded into VBA, and even in VB, I believe.

Sometimes it is usefull to use a DIM-statement for a single variable,
as this gives you some space on the right side of the code to insert
a comment for the specific variable. However, if you do not need
comments, then it is much easier to use the first example I gave.

You can also intermix datatypes in a single line in the following
manner, but I do not find that handy myself:

Dim strString1 as String, dblDouble1 as Double, iInteger1 as Integer

HTH,

CoRrRan

P.S. I use the "Hungarian"-notation (perhaps a small derivative) for
coding variables. Take a look at: http://tinyurl.com/wmwg (Link to
MS-support)
 
M

Myrna Larson

Not that I know of. You're free to make up your own, maybe wb, ws, rng, etc.


Is there listing of Hungarian notation for Excel specfic objects:
Workbook
WorkSheet
Range
etc.

CoRrRan said:
I apologize up front if this is a really dumb question.

My procedure [1 module/1 sub/900 lines (no comments)] involves 57
dim statements.

Lots of variables, but it runs great. It just looks strange with
all the Dim statements.

Is that too many? Is it bad coding practice? Any tips to do handle
it better?

Thank you,
Robert

Robert,

You can ignore using the DIM-statement for each variable in the
following manner:

Dim strString1 as String, strString2 as String, _
strString3 as String, strString4 as String
Dim iInteger1 as Integer, iInteger2 as Integer, iInteger3 as Integer
Dim sngSingle1 as Single, sngSingle2 as Single
Dim dblDouble1 as Double, dblDouble2 as Double

This way you can use one DIM statement to declare a number of
variables in a single line of code.

Do not use the following way, as it will provide you with a number of
unwanted Variant-datatypes:

Dim strString1, strString2, strString3 as String

In the above line, only the last variable (string 3) will have the
datatype "String", the other two are of datatype "Variant". This is
hardcoded into VBA, and even in VB, I believe.

Sometimes it is usefull to use a DIM-statement for a single variable,
as this gives you some space on the right side of the code to insert
a comment for the specific variable. However, if you do not need
comments, then it is much easier to use the first example I gave.

You can also intermix datatypes in a single line in the following
manner, but I do not find that handy myself:

Dim strString1 as String, dblDouble1 as Double, iInteger1 as Integer

HTH,

CoRrRan

P.S. I use the "Hungarian"-notation (perhaps a small derivative) for
coding variables. Take a look at: http://tinyurl.com/wmwg (Link to
MS-support)
 
R

Robert Lerner

That was very helpful. Thanks a bunch.

-Robert

CoRrRan said:
I apologize up front if this is a really dumb question.

My procedure [1 module/1 sub/900 lines (no comments)] involves 57
dim statements.

Lots of variables, but it runs great. It just looks strange with
all the Dim statements.

Is that too many? Is it bad coding practice? Any tips to do handle
it better?

Thank you,
Robert

Robert,

You can ignore using the DIM-statement for each variable in the
following manner:

Dim strString1 as String, strString2 as String, _
strString3 as String, strString4 as String
Dim iInteger1 as Integer, iInteger2 as Integer, iInteger3 as Integer
Dim sngSingle1 as Single, sngSingle2 as Single
Dim dblDouble1 as Double, dblDouble2 as Double

This way you can use one DIM statement to declare a number of
variables in a single line of code.

Do not use the following way, as it will provide you with a number of
unwanted Variant-datatypes:

Dim strString1, strString2, strString3 as String

In the above line, only the last variable (string 3) will have the
datatype "String", the other two are of datatype "Variant". This is
hardcoded into VBA, and even in VB, I believe.

Sometimes it is usefull to use a DIM-statement for a single variable,
as this gives you some space on the right side of the code to insert
a comment for the specific variable. However, if you do not need
comments, then it is much easier to use the first example I gave.

You can also intermix datatypes in a single line in the following
manner, but I do not find that handy myself:

Dim strString1 as String, dblDouble1 as Double, iInteger1 as Integer

HTH,

CoRrRan

P.S. I use the "Hungarian"-notation (perhaps a small derivative) for
coding variables. Take a look at: http://tinyurl.com/wmwg (Link to
MS-support)
 
R

Robert Lerner

That would work well for me because I have always used those very prefixes.
Great tip. Thanks.

-Robert

JE McGimpsey said:
57 variables in 900 lines sounds like a lot, but without seeing your
code, it's hard to say whether it's too many.

If the code is for your use only, my philosophy is usually "if it runs
the way you want, it's correct".

If the code will need to be maintained by others, you're better off
using a more formal system.

One shortcut, which I usually don't recommend, but can save a lot of
Dim'g is to use a DefType statement. For instance, at the module level,
putting

DefStr S

will automatically type all variables starting with "s" or "S" as
strings, so

Dim sOne, sTwo, sThree, sFour

is equivalent to

Dim sOne As String
Dim sTwo As String
Dim sThree As String
Dim sFour As String

or

Dim sOne As String, sTwo As String, sThree As String, sFour As String


Robert Lerner said:
My procedure [1 module/1 sub/900 lines (no comments)] involves 57 dim
statements.

Lots of variables, but it runs great. It just looks strange with all the
Dim
statements.

Is that too many? Is it bad coding practice? Any tips to do handle it
better?
 
Top