Results 1 to 4 of 4

Thread: [RESOLVED] run time error 9 subscript out of range ?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Resolved [RESOLVED] run time error 9 subscript out of range ?

    Hi all . i have an application that loads a playlist text file in to listboxes. The playlist text file has the song name and song path . When i try to load the playlist file i get the follwoing error

    Run-time error 9

    subscript out of range
    pointing at the following line:

    'Check for unwanted vbCrLf (which would yeild a "")
    If Len(strArr(intLB)) <> 0 Then

    could you guys help me fix this error? Looking forward for replies.Thanks


    2 Code:
    1. Private Sub Command1_Click()
    2.  
    3. Dim i As Long
    4. Dim intLB As Long   'Is an index counter
    5. Dim lngCtr As Long  'Represents the count of individual lines of data
    6. Dim strOpenPathFile As String, strArr() As String, strBuffArr() As String
    7.        
    8.     With CommonDialog1
    9.         .CancelError = True
    10.         On Error GoTo CommDiaCancelled
    11.         .Filter = "Text Files (*.txt)|*.txt|Word Doc (*.doc)|*.doc"
    12.         .ShowOpen
    13.     End With
    14.    
    15.     strOpenPathFile = CommonDialog1.FileName
    16.  
    17.     Open strOpenPathFile For Input As #1
    18.         strArr = Split(Input(LOF(1), 1), vbCrLf)
    19.     Close #1
    20.  
    21.     'Clear all the ListBoxs
    22.     List1.Clear
    23.     List2.Clear
    24.      lngCtr = UBound(strArr)
    25.  
    26.     For intLB = 0 To lngCtr
    27.    
    28. CommDiaCancelled:
    29.     If Err.Number = 32755 Then
    30.         MsgBox "Cancel selected !"
    31.         Exit Sub
    32.     End If
    33.         'Check for unwanted vbCrLf (which would yeild a "")
    34.         If Len(strArr(intLB)) <> 0 Then
    35.  
    36.             'Split the Line of data
    37.             strBuffArr = Split(strArr(intLB), vbTab)
    38.  
    39.             'Load the data into the ListBoxs
    40.  
    41.             List1.AddItem strBuffArr(0)
    42.             List2.AddItem strBuffArr(1)
    43.            
    44.         End If
    45.     Next
    46.     Call Number(List1, " - ")
    47. List1.Selected(0) = True
    48. For i = List1.ListCount - 1 To 0 Step -1 ' counts list entries
    49.     If List1.Selected(i) = True Then ' if selected then
    50.         Text1.Text = List1.List(i) ' add to other list
    51.         Text2.Text = List2.List(i)
    52.         Text3.Text = List1.List(i)
    53.         Text3.Text = Replace(Text3.Text, ".mp3", "")
    54.         Text3.Text = Replace(Text3.Text, ".avi", "")
    55.         Text3.Text = Replace(Text3.Text, ".asf", "")
    56.         Text3.Text = Replace(Text3.Text, ".mpeg", "")
    57.         Text3.Text = Replace(Text3.Text, ".mpg", "")
    58.         Text3.Text = Replace(Text3.Text, ".wav", "")
    59.         Text3.Text = Replace(Text3.Text, ".wmv", "")
    60.         Text3.Text = Replace(Text3.Text, ".wma", "")
    61.         Text3.Text = Replace(Text3.Text, ".cda", "")
    62.         Text3.Text = Replace(Text3.Text, ".mid", "")
    63.         Text3.Text = Replace(Text3.Text, ".midi", "")
    64.         Text3.Text = Replace(Text3.Text, ".rm", "")
    65.         RichTextBox3.TextRTF = Replace(RichTextBox1.TextRTF, "%song", Text3)
    66.         If Option2.Value = True Then
    67.         Else
    68.      
    69.         End If
    70.     End If
    71. Next
    72.  
    73. Text10.Text = List1.ListCount
    74. End Sub

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: run time error 9 subscript out of range ?

    1) What is this "CommDiaCancelled:" doing there in the for loop?
    2) What is the value of intLB at that moment of time??? Try and put this message box before that line to tfind the value of intLB....
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: run time error 9 subscript out of range ?

    You have at nearly the top: On Error GoTo CommDiaCancelled
    but CommDiaCancelled: is a label in middle of a For loop.
    Perhaps an error happens somewhere before the line: strArr = Split(Input(LOF(1), 1), vbCrLf)
    and the error handler jumps directly into the middle of the loop.
    At that point, intLB took the default value of 0 (the line "For intLB = 0 to ... " was not executed) and of course strArr(0) gives you subscript out of range because strArr() was not initialized.

    Why do you set the error handler jumps directly into the middle of the For Loop ? I don't know the reason.

    Perhaps the whole code block
    Code:
    CommDiaCancelled:
        If Err.Number = 32755 Then
            MsgBox "Cancel selected !"
            Exit Sub
        End If
    was copied and pasted to a wrong place.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: run time error 9 subscript out of range ?

    Thanks all i got it working . I had some textboxes/richtextbox missing in my form !! so i changed the last part of code to this and now working well :

    2 Code:
    1. ........
    2.  
    3.     'RichTextBox3.TextRTF = Replace(RichTextBox1.TextRTF, "%song", Text3)
    4.         'If Option2.Value = True Then
    5.         'Else
    6.      
    7.         'End If
    8.     End If
    9. Next
    10.  
    11. 'Text10.Text = List1.ListCount
    12. End Sub

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