[RESOLVED] subscript out of range???
If Val(strAll(lngIdxAll)) = 302 Then
ReDim Preserve strAll(lngIdxAll - 1)
Exit For
End If
when i run the program, it came out an error "subscript out of range" and point to the code (the blue color).
is it that not enough memory to execute the code??
Re: subscript out of range???
That error means that array's index is out of bound.
What's the current value of lngIdxAll conter?
If it is ZERO than you cannot redim array to a negative index so you may need to check if it is:
Code:
If lngIdxAll > 0 Then
If Val(strAll(lngIdxAll)) = 302 Then
ReDim Preserve strAll(lngIdxAll - 1)
Exit For
End If
End If
Re: subscript out of range???
Code:
For lngIdxAll = 1 To UBound(strAll)
If Val(strAll(lngIdxAll)) = 302 Then
ReDim Preserve strAll(lngIdxAll - 1) 'remove lines starting at START 302
Exit For
End If
above is my code
Re: subscript out of range???
You still haven't answered the simple question asked...
Re: subscript out of range???
This gives that error:
VB Code:
Dim lngIdxAll As Long
Dim strAll() As String
ReDim strAll(1 To 9)
strAll(5) = "302"
For lngIdxAll = 1 To UBound(strAll)
If Val(strAll(lngIdxAll)) = 302 Then
ReDim Preserve strAll(lngIdxAll - 1) 'remove lines starting at START 302
Exit For
End If
Next lngIdxAll
This does not give that error (see difference in line #4):
VB Code:
Dim lngIdxAll As Long
Dim strAll() As String
ReDim strAll(0 To 9)
strAll(5) = "302"
For lngIdxAll = 1 To UBound(strAll)
If Val(strAll(lngIdxAll)) = 302 Then
ReDim Preserve strAll(lngIdxAll - 1) 'remove lines starting at START 302
Exit For
End If
Next lngIdxAll
Either make sure that strAll is initially zero-based, or include the lower bound when you redim the array. ReDim Preserve strAll(1 To lngIdxAll - 1)