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
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
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