Search code examples
excelvbatxt

Importing a fixed width txt file into excel whilst transforming the row (date + Totals) into a column


I hope you can assist me I am a newbie in VBA and have quite extensive knowledge in Excel. I was wondering what would be the best way to import a fixed width text file into excel and generate the date on a column next to the transactions.

I am able to import the file into excel and place each field on its column however I am struggling to place the date next to each transaction as it is shown as a total at the bottom of each transactions as a total. Here is a dummy of the file below P.s the file is not tab delimited

Text file to be imported into excel


Solution

  • For simplicity lets asume the imported data is in 6 columns A-F, In the total line which contains the date, the first column has something like "Total for ..." and the second column contains the date leading with another text like "... - 18/12/2020".

    In column G of all lines you need the following formula, the code for cell G3:

    =IF(LEFT(A3,5)="Total",DATE(RIGHT(TRIM(B3),4),LEFT(RIGHT(TRIM(B3),7),2),LEFT(RIGHT(TRIM(B3),10),2)),G4)
    

    not using datevalue as I don't know the regional setting format in your PC, but I know the format in your file, which is DD/MM/YYYY