• 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 help in extracting Data from Text file and then converting it to text file in different format

Rahul1089

New Member
Dear Friends,

I have a .txt file that contains specific data that needs to be extracted and placed into respective columns in Excel. I am very much new to VBA coding and tried, but having difficulty in making this work... below shows the code I have thus far but when run, it is working differently. I want those data to be included in their respective field as sample in the excel. In the Excel file I have already kept the data as how it has to be fetched and filled in the respective headed column and then it has to be converted in the different format, which is in the attachment. The following is sample data from where the data needs to be extracted:-

TYPE;ACCOUNT NUMBER:BANK REFERENCE;BENEFICIARY NAME ;DATE ;AMOUNT ;BENE ACCOUNT NUMBER ;BENE IFSC ;BENE BANK NAME ;REFERENCE ;BENE MAIL ID
IMPS;45605104698 ;60062000057200 ;ABCDEF ;12122016;0000000001.00;10304060176 ;STRK0002018;STATE BANK OF INDIA ;5110845 ;abce@gmail.com ;

The code for extracting this above data is as below:-

Code:
Option Explicit

Sub importTXT()
Dim r As Range, myfile As Variant
Dim qt As QueryTable, i As Integer
Dim del As Range

'where myfile needs to select manually
myfile = Application.GetOpenFilename("All Files (*.*), **.*", _
, "Select TXT file", , False)
If myfile = False Then Exit Sub

'elseif its fixed
'myfile = "D:\windowsupdate1.txt"

Application.ScreenUpdating = False

With ActiveSheet
.Range("A7").CurrentRegion.Cells.Clear
With .QueryTables.Add(Connection:="TEXT;" & myfile, Destination:=.Range("$A$7"))
        .Name = "windowsupdate1"
        .TextFilePlatform = 437
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileTabDelimiter = True
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
'delete query tables if found any.
    For Each qt In ActiveSheet.QueryTables
        qt.Delete
    Next qt
'Delete the Data Connections
If .Parent.Connections.Count > 0 Then
    For i = 1 To .Parent.Connections.Count
        .Parent.Connections.Item(i).Delete
    Next i
End If

For Each r In .Range("F1:F" & .UsedRange.Rows.Count)
    If InStr(r, "Title = ") > 0 Then
        r.Offset(, 1) = Mid(r.Value, InStr(r, "Title = ") + 8, InStrRev(r.Value, " (KB"))
        r.Offset(, 2) = Mid(r.Value, InStrRev(r.Value, " (KB") + 2, Len(r.Value) - InStrRev(r.Value, " (KB") - 2)
    Else
        If del Is Nothing Then
            Set del = r
        Else
            Set del = Union(del, r)
        End If
    End If
Next
End With
Application.ScreenUpdating = False
End Sub

The data after extraction it should be filled in their respective column as below:-

SCHOOL CODESCHOOL NAMESTUDENT ACCOUNT NO.Debit Account CurrencyPayment MethodRemittance AmountRemittance CurrencyValue DateCustomer Ref No.Reference AuthorityPayment DetailsContract Number1Beneficiary Account No / Transfer Account NoBeneficiary's Name 1Beneficiary's Name 2Beneficiary Address 1Beneficiary Address 2Beneficiary Bank Name 1Bene Bank CodeBank ChargesCharge AccountE-mail addressDebit TypeInvoice Details
1452889​
XYZ
45605104698​
USDIMPS0000000001.00USD
12122016​
5110845​
60062000057200​
45869974582​
ABCDEFSTRK0002018abce@gmail.com

But it is not coming like as what I want.

After that the data needs to be again converted in the text file as below:-
“1452889”;“XYZ”;“45605104698”;“USD”;“IMPS”;“0000000001.00”;“USD”;“12122016”;“5110845”;“60062000057200”;“”;“”;“45869974582”;“ABCDEF”;“”;“”;“”;“”;“STRK0002018”;“”;“”;“abce@gmail.com”;“”;“”

And for doing so I am using the below code, but still not able to achieve the goal:-

Code:
Sub CopyDataToTextFile()
Dim X As Long, FF As Long, LastCopyRow As Long, DataToOutput As String
'First row of data to be extracted
Const StartRow As Long = 1
On Error Resume Next
LastCopyRow = Columns("A").SpecialCells(xlBlanks)(1).Row - 1
If Err.Number Then LastCopyRow = Cells(Rows.Count, "X").End(xlUp).Row
On Error GoTo 0
For X = StartRow To LastCopyRow
DataToOutput = DataToOutput & vbNewLine & Application.Trim(Join(Application.Index(Cells(X, "A").Resize(, 24).Value, 1, 0), vbTab))
Next
FF = FreeFile
Open "D:\ " & Range("A2").Value & ".txt" For Output As #FF
Print #FF, DataToOutput
Close #FF


End Sub

It would be highly Appreciate any help with this... many many thanks in advance. For better understanding I have attached the all files.
 

Attachments

  • sample file for DATA extraction.txt
    586 bytes · Views: 3
  • Student Mst record - Working.xlsm
    26.9 KB · Views: 3
  • required file format as an output.txt
    301 bytes · Views: 3
Last edited:
Hi !​
What is really needed :​
only to convert a text file to another text file (can be done without Excel via a Windows VBscript for example)​
or to first import a text file to a worksheet and in a second time create a new text file ?​
 
Hi !

Thanks a lot for the response,

Actually, first it has to be imported in the excel file from cell F7 to X7 from note pad data and then along with some data in the excel from A7 to X7 it has to be converted in the text.

Many many thanks a lot in advance if i get the solution.
 
Last edited by a moderator:
Below is the text file data and one by one data which is separated by semicolon needs to be inserted in the respective assigned cell no.

IMPS;45605104698 ;60062000057200 ;ABCDEF ;12122016;0000000001.00;10304060176 ;STRK0002018;STATE BANK OF INDIA ;5110845 ;abce@gmail.com ;

From text file, the first data IMPS in cell E7, second data 45605104698 in cell C7, third data 60062000057200 in cell J7, similarly ABCDEF in N7, 12122016 in H7, 0000000001.00 in F7, 10304060176 in M7, STRK0002018 in S7, STATE BANK OF INDIA in R7, 5110845 in I7, and abce@gmail.com in V7.

This is what I required to be inserted from text file into the excel cells.
 
Last edited by a moderator:
Hi, Marc

Thank you very much,

For better understanding please open the text file "sample file for Data Extraction". from this file it has to be get imported in the excel first.
 
When a thread does not receive any help then it might not be as clear as necessary like described in the forum rules …​
Join the source workbook (before any procedure execution),​
join the result workbook (or an additional worksheet well identified in the source workbook) as it must be after the procedure​
and attach the source text file and the expected text file in a manner all these files are relevant between each other.​
Of course with a complete crystal clear explanation of the need, the more accurate, the less mods you may have to proceed further …​
 
Back
Top