To declare variables in VBA
, I always used the following method: all variables of the same type I would put on a same line and declare their type only at the end, as in the example below:
Dim RngSource, RngDestin, RngCalc As Range
Dim i, j As Long
Dim n As Integer
Dim Tot1, Tot2, Cust As String
Recently, searching the internet, I noticed that some sources say that this way of declaring variables is wrong, since only the last variable is sized by the declared type. For example, in the Dim i, j As Long
line, only the variable j
will be scaled as Long
, whereas the i
variable would be being scaled as the generic variable Variant
. Supposedly, the correct form of statement would be:
Dim RngSource As Range, RngDestin As Range, RngCalc As Range
Dim i As Long, j As Long
Dim n As Integer
Dim Tot1 As String, Tot2 As String, Cust As String
However, I still find sources on the internet recommending the previous model to shorten the size of the code.
After all: what is the correct way?