Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

subscript out of range..? 1

Status
Not open for further replies.

marduk813

Programmer
Jul 18, 2002
89
US
I'm getting a "subscript out of range" error on the following code and I can't for the life of me figure out why.

Code:
' General declarations
Option Base 1
Public varPay as Variant


Public Sub Userform_Initialize()

    varPay = Array(4)

End Sub

Public Sub cmdAdd_Click()

    Dim intCount, intTemp as Integer

    intTemp = 10
    varPay(1) = 10
    For intCount = 2 To 4
        If intTemp < 22 then
            intTemp = intTemp + 7
        Else
            intTemp = intTemp - 21
        End If
Code:
        varPay(intCount) = intTemp
Code:
     Next intCount
    .
    .
    .
    .

End Sub

The line marked in red is where I'm getting the error. The error occurs on the first pass through the For..Next loop when intCount = 2. I don't understand how a subscript of 2 can be out of range when the array has an upper boundary of 4.

Thanks in advance,

Jas
 
try amending your code to this :-

Option Base 1
Public varPay(4) As Variant

'
'Public Sub Userform_Initialize()
'
' varPay = Array(4)

'End Sub

Public Sub cmdAdd_Click()

Dim intCount, intTemp As Integer

intTemp = 10
varPay(1) = 10
For intCount = 2 To 4
If intTemp < 22 Then
intTemp = intTemp + 7
Else
intTemp = intTemp - 21
End If
varPay(intCount) = intTemp
Next intCount

End Sub Dom
 
Dom, thanks for the reply, but I tried it your way first, and got an error saying
&quot;Constants, fixed-length strings, arrays, user-defined types, and Declare statements not allowed as Public members of object modules&quot;,
which is why I resorted to using the Array statement.


Jas
 

Ok, the reason that you are getting the subscript out of range error is because the array you are trying to traverse has only one element at index one (1) with a value of 4.
Meaning, when you do ...
[tt]
varPay = Array(4)
[/tt]
varPay has only one element at index 1 with a value of 4. This does not mean that there are 4 elements to the array.
[tt]
var = varPay(1) 'var will now equal 4
[/tt]
Now to correct your code to dimension your array to have 4 elements you can use ManxDom's suggestion except the declaration needs to be private or dim since it is in a form module or you can use a varient of your origional code.
[tt]
varPay = Array(0, 0, 0, 0)
[/tt]
This will dimension your array to 4 elements and initialize your array so each elements value equals zero.

I hope this helps, Good Luck

 
Well I'll be shoddy programmer. You're right! I misunderstood the help section for the Array function. After rereading it, I don't know how I could have misinterpreted it, but I did.
It works great now. Thanks vb5.


Jas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top