Results 1 to 5 of 5

Thread: [RESOLVED] subscript out of range???

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    120

    Resolved [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??

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    120

    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

  4. #4
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: subscript out of range???

    You still haven't answered the simple question asked...

  5. #5
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: subscript out of range???

    This gives that error:
    VB Code:
    1. Dim lngIdxAll As Long
    2. Dim strAll() As String
    3.  
    4. ReDim strAll(1 To 9)
    5. strAll(5) = "302"
    6. For lngIdxAll = 1 To UBound(strAll)
    7.   If Val(strAll(lngIdxAll)) = 302 Then
    8.     ReDim Preserve strAll(lngIdxAll - 1)   'remove lines starting at START 302
    9.     Exit For
    10.   End If
    11. Next lngIdxAll

    This does not give that error (see difference in line #4):
    VB Code:
    1. Dim lngIdxAll As Long
    2. Dim strAll() As String
    3.  
    4. ReDim strAll(0 To 9)
    5. strAll(5) = "302"
    6. For lngIdxAll = 1 To UBound(strAll)
    7.   If Val(strAll(lngIdxAll)) = 302 Then
    8.     ReDim Preserve strAll(lngIdxAll - 1)   'remove lines starting at START 302
    9.     Exit For
    10.   End If
    11. 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)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width