To get List of Positions of different strings in a text file in a Textbox - Part 1 After getting the positions i would ...... Part 2

NimishK

Well-known Member
Joined
Sep 4, 2015
Messages
684
Hello

Part 1
As the following code results for Single searched String and getting its Position in Line Number of a text file.
I was wondering if a List of Array of Strings could be searched and getting Position of Each String with Line number one below another in a text box as per below Result in quote marks

MySearch = GetSearchResults("C:\NimishK\strPos.txt", "RefID:")
I wanted to use the above with below Syntax

MySearch = GetSearchResults("C:\NimishK\strPos.txt", arrayStringsRev(j))

Miserably failed in deriving the result
MrExcel Thread: 1148864

VBA Code:
Module:
Public Type SearchResults
    LineNumber As Long
    CharPosition As Long
    StrLen As Long
End Type

Option Explicit

Private Sub UserForm_Initialize()
   Call Example
End Sub

Sub Example()
Dim txtFileName As String, strData As String
Dim MySearch() As SearchResults, i As Long

Dim arrayStringsRev(4) As String
arrayStringsRev(1) = "RefID:"
arrayStringsRev(2) = "Name:"
arrayStringsRev(3) = "Area"
arrayStringsRev(4) = "Amount"


   For j = LBound(arrayStringsRev) To UBound(arrayStringsRev)
     MySearch = GetSearchResults("C:\NimishK\strPos.txt", arrayStringsRev(j))
  
   ''''' MySearch = GetSearchResults("C:\NimishK\strPos.txt", "RefID:")
    For i = 0 To UBound(MySearch)
        With MySearch(i)
        strData = " is in line " & .LineNumber & " Position " & .CharPosition & " Length " & .StrLen

        End With
    Next
    j = j + 1
    Next

    txtBox4.Text = txtBox4.Text & strData
End Sub

Function GetSearchResults( _
  FileFullName As String, _
  FindThis As String, _
  Optional CompareMethod As VbCompareMethod = vbBinaryCompare) _
  As SearchResults()
 
    Dim fso As New FileSystemObject, s As String, pos As Long, l As Long, sr As SearchResults, ret() As SearchResults, i As Long
   
    With fso.OpenTextFile(FileFullName)
        Do Until .AtEndOfStream
            l = l + 1
            s = .ReadLine
            pos = 1
            Do
                pos = InStr(pos, s, FindThis, CompareMethod)
                If pos > 0 Then
                    sr.CharPosition = pos
                    sr.LineNumber = l
                    sr.StrLen = Len(FindThis)
                    ReDim Preserve ret(i)
                    ret(i) = sr
                    i = i + 1
                    pos = pos + 1
                End If
            Loop Until pos = 0
        Loop
    End With
    GetSearchResults = ret
End Function

Result
RefID: is in line 1 Position 25 Length 6
Name: is in line 2 Position 5 Length 5
Area: is in line 2 Position 15 Length 5
Amount is in line 3 Position 15 Length 6

Part 2:
After getting the positions i would like to Carrage Return / vbcrlf/vbNewline at the list of positions

Will always appreciate your help

Thanks
NimishK

 
OK
Another change i made ie
Dim MySearch(1 To 4) As SearchResults
because Dim MySearch() SearchResults as Nothing

The above result displayed with 2 repeatations but no new line nos, char position and string len were displayed
if the strings as per arrayStringsRev(j) are repeated in text file. This seems difficult

NimishK
 
Upvote 0

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Below is the code I am using, to produce these results.
Note: if the file is an automated file then I would expect:-
• Line no - to change
• Position - to stay the same - This is a label and will most likely appear in the same position of a line each time
• Length - to stay the same - It is the length of what you are searching for not the length of the line - so it won't change

1631620629392.png


I only changed the Example Sub so here is what I am using:-

VBA Code:
Sub Example()
Dim txtFileName As String, strData As String
Dim MySearch() As SearchResults, i As Long

Dim arrayStringsRev(1 To 4) As String
arrayStringsRev(1) = "RefID:"
arrayStringsRev(2) = "Name:"
arrayStringsRev(3) = "Area"
arrayStringsRev(4) = "Amount"


   For j = LBound(arrayStringsRev) To UBound(arrayStringsRev)
     MySearch = GetSearchResults("C:\NimishK\strPos.txt", arrayStringsRev(j))
     
   ''''' MySearch = GetSearchResults("C:\NimishK\strPos.txt", "RefID:")
    For i = 0 To UBound(MySearch)
        With MySearch(i)
            strData = strData & arrayStringsRev(j) & ": is in line " & .LineNumber & " Position " & .CharPosition & " Length " & .StrLen & vbLf

        End With
    Next i

    Next j
    
    ' Debug.Print strData ' I tested using this line and not the txtBox4 line since I don't have the form
    
    txtBox4.Text = txtBox4.Text & strData
End Sub

My test data:-
XX
Apple
RefID:
Name:
Orange
Test-More
Area
-one more
Amount
XX
Apple
Test pos RefID: and length
Name:
Orange
Test-More
Test pos Area and length
-one more
Amount
 
Upvote 0
Solution
Alex
Below is the code I am using, to produce these results.
Note: if the file is an automated file then I would expect:-
• Line no - to change
• Position - to stay the same - This is a label and will most likely appear in the same position of a line each time
• Length - to stay the same - It is the length of what you are searching for not the length of the line - so it won't change

Just Perfect One. Thanks a Tonne. ??
I dont know where i missed although need to raise confidence into developing of functions. The great part is that nothing required to change the function
So Part 1 completes.

NimishK
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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