F0l0w
F0l0w

Reputation: 261

vbScript - Arrays

I am fairly new in vbScript and was wondering if anyone could please advise:

I am trying to loop for a single value in an array. See the following:

Dim reportesTA(3)
reportesTA(0) = "REPORT1"
reportesTA(1) = "REPORT2"
reportesTA(2) = "REPORT3"

I wrote a Sub procedure as follow:

Sub REPORTE1()
    For Each i=0 In reportesTA
        posicionarCursor() ''This is another sub
        commKeys reportesTA(0)
    Next
End Sub

It won't work and I am not sure what I am doing wrong. The other way I though to do this would be:

For i=0 to 0
    posicionarCursor() ''This is another sub
    commKeys reportesTA(0)
Next

Upvotes: 0

Views: 408

Answers (2)

Ali Sheikhpour
Ali Sheikhpour

Reputation: 11045

There are two syntax to loop through an array:

For Each i In reportesTA
' your code here
next

or this one:

for i=0 to ubound(reportesTA)
' your code here
next

Upvotes: 0

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

The size of an array is given by the zero-based last valid index, not the number of elements; so

>> Dim rTA(2) ' 3 elms from 0 to 2
>> rTA(0) = "1"
>> rTA(1) = "2"
>> rTA(2) = "3"

To loop over an array, either use a count/index loop:

>> For i = LBound(rTA) To UBound(rTA)
>>     WScript.Echo i, rTA(i)
>> Next
>>
0 1
1 2
2 3

and access the elements via array(index), or a For Each loop

>> For Each r In rTA
>>     WScript.Echo r
>> Next
1
2
3

giving you access to (a copy of) each element without having to use an index.

Upvotes: 2

Related Questions