PDA

View Full Version : [SOLVED] Combine multiple DIMs into one line instead of separate?



ashleyuk1984
11-23-2017, 06:38 AM
Hi,
I'm wondering if I'm actually doing this correct or not?

Instead of declaring my DIMs on seperate lines like:


Dim x As Integer
Dim LastRow As Integer
Dim i As Integer

I would do this instead....


Dim x, LastRow, i As Integer

Is this correct?? Because when I step through my code with F8, I notice that they are set as Variable/Empty in the Locals window?? Not set as Integer ?

Am I doing it correctly? Or should I be doing it differently?

Thanks

Logit
11-23-2017, 07:15 AM
.
From : http://www.cpearson.com/excel/declaringvariables.aspx




Pay Attention To Variables Declared With One Dim Statement
VBA allows declaring more than one variable with a single Dim statement. I don't like this for stylistic reasons, but others do prefer it. However, it is important to remember how variables will be typed. Consider the following code:
Dim J, K, L As Long
You may think that all three variables are declared as Long types. This is not the case. Only L is typed as a Long. The variables J and K are typed as Variant. This declaration is functionally equivalent to the following:
Dim J As Variant, K As Variant, L As Long
You should use the As Type modifier for each variable declared with the Dim statement:
Dim J As Long, K As Long, L As Long


Another resource : http://learnexcelmacro.com/wp/2012/10/declaring-multiple-variables-with-one-dim-statement/


And finally from the 'creator' itself : https://msdn.microsoft.com/en-us/vba/language-reference-vba/articles/declaring-variables



In the following statement, the variables intX, intY, and intZ are declared as type Integer.

Dim intX As Integer, intY As Integer, intZ As Integer
In the following statement, intX and intY are declared as type Variant, and only intZ is declared as type Integer.
Dim intX, intY, intZ As Integer
You don't have to supply the variable's data type in the declaration statement. If you omit the data type, the variable will be of type Variant.

Paul_Hossler
11-23-2017, 07:49 AM
Yes, but not exactly -- depends on what you mean by 'right' -- see Logit's post and links, esp the part about variables not explicitly Dim-ed as a specific type



To ensure that the macro works as intended, I think (IMVHO) it's better defensive programming to explicitly Dim everything, even on one line




Dim x as Integer, LastRow as Integer, i As Integer



That way later in the macro if I make a mistake and use




.....

LastRow = "Done"

.....

LastRow = LastRow + 1





I'll get a message on the assignment line since "Done" cannot convert to a number

ashleyuk1984
11-23-2017, 08:20 AM
Ahhhh, that's where I'm going wrong.
Ok that makes sense. I was sure that I saw somewhere that you could declare them on one line just for 'neatness'... But I was doing it slightly wrong.


Dim x As Integer, LastRow As Integer, i As Integer
This is the approach that I was looking for.

Thank You Logit and Paul !

Aflatoon
11-23-2017, 08:24 AM
If you want, you can also use type declaration characters:


Dim x%, LastRow%, i%

or even a Def type statement like


DefInt I

then any variable whose name begins with i is automatically an Integer unless otherwise specified.

Just as an aside, you should always use Long for row number variables.

Logit
11-23-2017, 08:55 AM
.
You are welcome. Glad to help.

For what its worth, I sometimes 'forget' and do it wrong as well. :banghead:

POO!

SamT
11-24-2017, 11:59 AM
IMNSHO It is best programming practice to declare all on separate lines, grouped by subject, then alphabetized.

Makes it easy to research when you next have to maintain the code.

SamT's Rule # 1: Write your code like an instruction manual.
Rule # 2, If you don't understand Rule #1, Write your code like Microsoft.

Also, if I use mnemonic Names, I comment the meaning for accurate mental association

Dim EmpFNm As String 'Employee First Name
Dim EmpLNm As String 'Employee Last Name
Dim EmpA1 as String 'Employee First or Street Address
Dim EmpA2 As String 'Employee Address 2 or Extra line
Dim EmpA3 As String 'Employee City
Dim EmpA5 As String(2) 'Employee State Abbreviation
Dim EmpA6 As String(10) 'Employee full Zip Code (12345-1234)

Paul_Hossler
11-24-2017, 12:41 PM
@SamT -- Syntax for fixed length strings was wrong

I'd probably use classes, but if you wanted to keep it more simple, you could use user defined Types



Option Explicit

Type Employee
FirstName As String
LastName As String
StreetAddress1 As String
StreetAddress2 As String
City As String
State As String * 2
ZipCode As String * 5
MgrFirstName As String
MgrLastName As String
End Type

Sub TypeDemo()
Dim Manager As Employee, Worker As Employee

With Manager
.FirstName = "Tom"
.LastName = "Jones"
End With

With Worker
.MgrFirstName = "Tom"
.MgrLastName = "Jones"

.FirstName = "Mary"
.LastName = "Smith"
.StreetAddress1 = "123 W. 4th Ave"
.City = "Hometown"
.State = "DC"
.ZipCode = "12345"
End With

With Worker
MsgBox .FirstName & " " & .LastName & " works for " & .MgrFirstName & " " & .MgrLastName
End With


End Sub

SamT
11-24-2017, 01:39 PM
String size format? I figgered. But it's valid in some language. I just look it up when I rarely need it

And I too love the simpleness and capabilities of Classes.