Hello
Actually i would like to return Line number and the position number of cursor with length of string searched From Text File
I tried the following code from
StackOverflow thread 23369908
Basically the above thread showed the position of string searched for and as on one line only
Where as i would like msgbox to show "String Searched" Amt
in line 3 Position 1 Lenght 3
in line 7 Position 1 Lenght 3
in line 11 Position 1 Lenght 3
Example "String Searched" 8600
in line 7 Position 5 Lenght 4
Example "String Searched" 0X
in line 6 Position 21 Lenght 2
in line 10 Position 24 Lenght 2
Example "String Searched" 09
in line 1 Position 9 Lenght 2
in line 4 Position 9 Lenght 2
in line 10 Position 33 Lenght 2
Structure of "C:\ABC\TextFile.txt"
Your help will be appreicated
SamD
162
Actually i would like to return Line number and the position number of cursor with length of string searched From Text File
I tried the following code from
StackOverflow thread 23369908
Basically the above thread showed the position of string searched for and as on one line only
Where as i would like msgbox to show "String Searched" Amt
in line 3 Position 1 Lenght 3
in line 7 Position 1 Lenght 3
in line 11 Position 1 Lenght 3
Example "String Searched" 8600
in line 7 Position 5 Lenght 4
Example "String Searched" 0X
in line 6 Position 21 Lenght 2
in line 10 Position 24 Lenght 2
Example "String Searched" 09
in line 1 Position 9 Lenght 2
in line 4 Position 9 Lenght 2
in line 10 Position 33 Lenght 2
Structure of "C:\ABC\TextFile.txt"
Date 19-09-2020
Reference Number NZ26X201249Y75140
Amt 2000.00
Date 28-09-2020
Reference Number MD00X1485Y47366
Amt 8600.00
Date 10-10-2020
Reference Number YN27220X1562M9109
Amt 7650.00
VBA Code:
Option Explicit
Public myFile As String
Public textLine As String
Private Sub UserForm_Initialize()
myFile = "C:\ABC\TextFile.txt"
Open myFile For Input As #1
Do Until EOF(1)
Line Input #1, textLine
TextBox2.text = TextBox2.text & textLine & vbCrLf
Loop
Close #1
End Sub
Private Sub CommandButton1_Click()
Call REVSrchStr_Load
End Sub
Private Sub RevSrchStr_Load()
Dim myFile As String
Dim text As String
Dim textLine As String
Dim posIAV As Integer
Dim Ctr As Integer
Dim Ctr2 As Integer
Dim data() As String
myFile = "C:\ABC\TextFile.txt"
Ctr = 0
Ctr2 = 0
Open myFile For Input As #1
Do While Not EOF(1)
Line Input #1, textLine
text = text & textLine & vbCrLf
' Increment Ctr since you're on the line now
Ctr = Ctr + 1
' Check the current line to see if it contains the string we're looking for
' If it does, set Ctr2 to be the current line number
If textLine Like TextBox1.text Then
Ctr2 = Ctr
End If
Loop
Close #1
posIAV = InStr(text, TextBox1.text)
MsgBox (posIAV) & " in number Line : " & Ctr
End Sub
SamD
162
Last edited: