Declaring variables

chuckles1066

Banned
Joined
Dec 20, 2004
Messages
372
Just posting on from something I read earlier.

Code:
Dim a, b, c, d as Integer

Only d is given the type integer in this example whereas I assumed a, b and c would also be type integer?

So what type are a, b and c?

I'm a bit concerned because, obviously, my misunderstanding might have adverse or unpredictable consequences in some of the macros I've written.

TIA.
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Variables whose data types are not specified are Variants.
 
Upvote 0
That declares a, b and c as Variant and d as Integer. You should use

Code:
Dim a As Integer, b As Integer, c As Integer, d As Integer
 
Upvote 0
Thanks both.

So, stating the bleeding obvious (as Basil Fawlty would say)

Code:
Dim a, b, c, d, e as Boolean

is going to cause someone a lot of grief if a, b, c and d are only ever intended to be True or False?
 
Upvote 0
A Variant can contain anything, so it's not going to intrinsically cause a problem, but it's sloppy practice to not strongly type most variables. If you need a Variant (and sometimes you will), declare the variable as a Variant.

There's an option in the VBE (Tools > Options) to require variable declaration. You should tick it.
 
Upvote 0
You should always put
Code:
Option Explicit
at the top of your module to make sure you declare all your variables (as it will allow your code to be faster if explicitly stated)

Moreover, to not tire your fingers, you can use variable identifiers such as %,&, #, $.

This will allow your code to appear much shorter and easier to read (imo)

VARIABLE IDENTIFIER
Interger = %
Long = &
Currency = @
Single = !
Double = #
String = $

So, you would use them like this
Code:
Dim a%, b%, c%, d%
to declare as integers.
 
Upvote 0
I would not declare variables like that (using type declaration characters) unless my disk drive had less than 5MB of storage.
 
Upvote 0
shg, may I ask why not?
I have just found out about the type declaration characters and do not know the drawbacks of them or the advantages as a matter of fact..
 
Upvote 0
Computers execute code, but only people read it. Code should be written to be easy to understand.

Declaring variables also affords the opportunity to explain usage:

Code:
Function AlignKeys1(wks As Worksheet) As Boolean
    Dim rKey        As Range    ' cells in header row containing the first column of each dataset
    Dim cell        As Range    ' For Each loop control variable
 
    Dim iRow        As Long     ' row index
    Dim iCol        As Long     ' column index
    Dim aiCol()     As Long     ' array containing the column indices of Keys
 
    Dim ar()        As Range    ' an array of ranges containing each of the datasets to be aligned
    Dim iRng        As Long     ' index to range array
    Dim nRng        As Long     ' number of ranges

IMO, an appropriate usage for a type declaration is for literal constants:

Code:
    Dim d As Double
    Dim i As Long
 
    d = 1#
    i = 1&
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,828
Members
452,946
Latest member
JoseDavid

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top