Loop Through Array Backwards?

Pascal

Board Regular
Joined
Jun 6, 2007
Messages
200
Hi,

This question is asked at a tail end of another thread regarding reading from a file, so I thought I'd start a new thread regarding this specific question.

I have the following that is reading the lines of a file into the Array: -

Code:
Sub ReadStrangeFile()
     ' Requires a reference to Microsoft Scripting Runtime (Tools > References)
    Dim FSO As FileSystemObject
    Dim FSOFile As File
    Dim FSOStream As TextStream
    Dim arrFileLines() As String
    Dim i As Integer
     
    Set FSO = New FileSystemObject
    Set FSOFile = FSO.GetFile(strLogFile)
    Set FSOStream = FSOFile.OpenAsTextStream(ForReading, TristateUseDefault)
    i = 0
    Do While Not FSOStream.AtEndOfStream
        ReDim Preserve arrFileLines(i)
        arrFileLines(i) = FSOStream.ReadLine
        i = i + 1
    Loop
    FSOStream.Close
    
    Debug.Print UBound(arrFileLines())
        
End Sub

Now that the Array has been populated, I'd like to start from the bottom/end of the array and work up to look for a line from the file imported that contains "System:"

Here is a sample of such a line from the file: -

19:47:31} System:11(Blau Aescs PM-L b37-11) Body:0 Pos1.13764e+010,7.24941e+009,-7.96476e+009) cruising

If such a line is found I then need to grab just "Blau Aescs PM-L b37-11" from it.

I don't want to start from the top as I'm always only interested in the last entry in the file of such a line, of which there will be many, the test file I'm working with currently has over 5,000 lines in it and some others over 9,000.

In VB.Net I was able to use: -

Code:
Array.Reverse()

As per this snippet of code: -

Code:
        strLines.Reverse()

        For Each strLine In strLines
            strTextLine = strLine
            If strTextLine.Contains("System:") Then
                intFirstPos = InStr(1, strTextLine, "(", CompareMethod.Text)
                intLastPos = InStr(1, strTextLine, ")", CompareMethod.Text)
                intLength = intLastPos - intFirstPos
                strCurrSystem = Mid(strTextLine, intFirstPos + 1, intLength - 1)
                Exit For
            End If
        Next

However, unfortunately this isn't available within VBA, hence trying to find another way around it.

Does anyone know how this can be done, or an alternative to achieve the result I'm after?

Thanks in advance.

Regards
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hi

What you want can be done, something like this will do it:

Code:
For i = UBound(arrLineFiles) To 0 Step -1

    If InStr(1, MyArr(i), "System:") > 0 Then
        'insert your code here
        Exit For
    End If

Next i

I trust that helps.
Andrew
 
Upvote 0
Sorry - I got distracted part way through,

Change MyArr to arrLinesFile.
 
Upvote 0
Hi

What you want can be done, something like this will do it:

Code:
For i = UBound(arrLineFiles) To 0 Step -1

    If InStr(1, MyArr(i), "System:") > 0 Then
        'insert your code here
        Exit For
    End If

Next i

I trust that helps.
Andrew

Good Morning,

Many thanks for your reply, works like a charm!

Best Regards
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,688
Members
448,978
Latest member
rrauni

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top