error run time 9

User avatar
sal21
PlatinumLounger
Posts: 4364
Joined: 26 Apr 2010, 17:36

error run time 9

Post by sal21 »

i use this code to loop txt file:

Code: Select all


Private Sub Command1_Click()

    Dim L As Long
    Dim MyArray() As String

    FileToArray "C:\TEMP\TABULATI_132.TXT", MyArray

    ReverseStrArray MyArray

    For L = 0 To UBound(MyArray)
        Debug.Print MyArray(L)
    Next L

End Sub
Private Sub FileToArray(ByVal sPath As String, ByRef sArray() As String)

    Dim ff As Integer
    
    ff = FreeFile
    On Error GoTo Fini
    Open sPath For Input As #ff
    sArray = Split(Input(LOF(ff), ff), vbCrLf)
Fini:
    Close #ff

End Sub
Private Sub ReverseStrArray(ByRef sArray() As String)

    Dim ubnd As Long, lbnd As Long, x As Long
    Dim sTmp As String
    
   [b] ubnd = UBound(sArray)[/b]
    lbnd = LBound(sArray)
    
    For x = lbnd To ((ubnd - lbnd - 1) \ 2)
        sTmp = sArray(lbnd + x)
        sArray(lbnd + x) = sArray(ubnd - x)
        sArray(ubnd - x) = sTmp
    Next x

End Sub
but have error 9 (see image) in ubnd = UBound(sArray)

Note:
my txt file have approx 2.600.000 lines;-(

User avatar
HansV
Administrator
Posts: 78562
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: error run time 9

Post by HansV »

With such a large file, processing it will always be slow.

Storing the entire file in an array will take up a lot of memory. It might be more efficient to process it line by line, and depending on the capacity of your computer it could be faster.
Best wishes,
Hans