PDA

View Full Version : Variable declaration



Mihai_Norsk
10-02-2018, 01:02 AM
Hi all,

I'm trying to "read" the code of a colleague and I'm stock in a thing.

He declared a variable like you can see below and I can not figure out the scope of it.

Maybe you can help me.



Dim variable(100, 1) as String


I do not know what do the two values between parenthesis.

Thank you in advance!

Aflatoon
10-02-2018, 04:37 AM
It's a String array of 101 rows and 2 columns (unless there's an Option Base 1 statement at the top of the module).

Paul_Hossler
10-02-2018, 06:35 AM
Fully expanded it's


Dim VariableName (0 to 100, 0 To 1) As String

for a total of 202 Strings (101 x 2)

Unless as Aflatoon says there's an


Option Base 1

at the top (outside of subs) of the module, it's assumed as Option Base 0, so any unspecified lower limits are assumed to start at 0

Mihai_Norsk
10-02-2018, 07:23 AM
Hi,

Thanks a lot to both of you.

I have not known that Dim variable_name(0 to 100, 0 to 1) as String can be written Dim variable_name(100, 1) as String.

To be honest I've used more one dimension arrays than two dimension ones, that's why my childish question and now if I remember correctly, in case of one dimension array, Dim variable(0 to 10) as String is the same with Dim variable(10) as String so I could have done the analogy if I was smart enough.

Regarding Option Base 1, is was not the case in my example but thanks for reminding the importance of it.

All the best!

Paul_Hossler
10-04-2018, 08:54 AM
Regarding Option Base 1, is was not the case in my example but thanks for reminding the importance of it.


I don't use it myself, but sometimes I'll inherit a macro where it is used. I always use LBound(…) even if I 'know' it's Base 0

As a second reminder, the (0 To N) is not written is stone

Sometimes I need to (say) move rows 20 to 29 into an array

Dim Ary (20 To 29) can simplify the macro and make it easier to read




For I = LBound(Ary) to UBound(Ary)
Ary(I) = Cells(I, 1).value
Next I







if I was smart enough.

No, that's called experience .. just keep on VBA-ing