• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Need to Extract Specific Data out of a .txt file

Status
Not open for further replies.

Zmorris

New Member
Hello,

I am looking to track specific information in regards to a text file. I really only need one piece of information from all the text that is generated in my file. I am specifically looking to put the value after every "s/n=" in an excel spread sheet. I want this text to start in A2 and go on from there. Any help would be greatly appreciated. I have placed a sample text file below. Thanks.
 

Attachments

  • 01-12-15 DDR2.txt
    55.1 KB · Views: 16
Zmorris

Firstly, Welcome to the Chandoo.org Forums

Please try the following code
Copy and paste it into a Code Module in VBA
Change the path/filename to suit

Code:
Sub Sample()
Dim MyData As String
Dim strData() As String
Dim i As Long

Dim Ret

Ret = Application.GetOpenFilename("Text Files (*.txt), *.txt")

If Ret = "" Then Exit Sub

Open Ret For Binary As #1
  MyData = Space$(LOF(1))
  Get #1, , MyData
Close #1
strData() = Split(MyData, vbCrLf)

Line = 2
For i = LBound(strData, 1) To UBound(strData, 1)
If InStr(1, strData(i), "s/n=") Then
  Cells(Line, 1) = Right(strData(i), Len(strData(i)) - InStr(1, strData(i), "="))
  Line = Line + 1
End If

Next i
Columns("A:A").EntireColumn.AutoFit

End Sub

I get:
upload_2016-9-14_14-10-1.png

See attached file:
 

Attachments

  • Import Text File.xlsm
    15.1 KB · Views: 12
Last edited:
Zmorris

Firstly, Welcome to the Chandoo.org Forums

Please try the following code
Copy and paste it into a Code Module in VBA
Change the path/filename to suit

Code:
Sub Sample()
Dim MyData As String
Dim strData() As String
Dim i As Long

Dim Ret

Ret = Application.GetOpenFilename("Text Files (*.txt), *.txt")

If Ret = "" Then Exit Sub

Open Ret For Binary As #1
  MyData = Space$(LOF(1))
  Get #1, , MyData
Close #1
strData() = Split(MyData, vbCrLf)

Line = 2
For i = LBound(strData, 1) To UBound(strData, 1)
If InStr(1, strData(i), "s/n=") Then
  Cells(Line, 1) = Right(strData(i), Len(strData(i)) - InStr(1, strData(i), "="))
  Line = Line + 1
End If

Next i
Columns("A:A").EntireColumn.AutoFit

End Sub

I get:
View attachment 34485

See attached file:
Dear hui please help me too as i have a similar problem actually i want to extract data from MT103 Swift Message which is exported from Swift.
i need the clauses as headers and the data is under the clause in row as a tabular form.
 

Attachments

  • MT 103 030919.txt
    12.5 KB · Views: 2
Status
Not open for further replies.
Back
Top