Link to home
Start Free TrialLog in
Avatar of albasatar
albasatarFlag for Malaysia

asked on

How to import a fixed length file AND export to a fixed length in excel?

Hi,

I have a fixed-length text file, with the following individual field, depending on the line:

1 ABCD XXXX YYYY (4 FIELDS)
2 ABCDEFG HIJKLM  (3 FIELDS)

I want to be able to import the file, do some computation, and now it becomes:
1 DCBA YYYY XXXX (4 FIELDS)
2 MNKJIH GFEDCBA (3 FIELDS)

and finally be able to export the content  to another text file. Note that the new file needs to be machine readable
Avatar of albasatar
albasatar
Flag of Malaysia image

ASKER

Note that this has to be a vba solution as i have big files to process
ASKER CERTIFIED SOLUTION
Avatar of Dana Seaman
Dana Seaman
Flag of Brazil image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@albasatar

The example you posted is not in a fixed length field format.
It is a fixed length, but varies from record to record. It will be interpreted according to the position of each field.

1 DCBA YYYY XXXX (4 FIELDS)
2 MNKJIH GFEDCBA (3 FIELDS)

No.  What you posted is a space-delimited fomat.

The following is an example of fixed length format.
1 DCBA        YYYY       XXXX (4 FIELDS)
2 MNKJIH      GFEDCBA         (3 FIELDS)

Open in new window

Ya, apologies, it is space-delimited rather than fixed length.

The code above does not work, unfortunately.
@albasatar

>>The code above does not work

Please add details on what you need.  For instance...
1. will there be multiple output files?  If so, what criteria do you use for each?
2. are there only two line formats (4 fields and 3 fields)
3. in what ways does the code "not work"?
@danaseaman

I suspect you will need to use the LIMIT parameter of the SPLIT() function to separate the first 'word' from the rest of the line.  Just have to wait for the OP response about processing details.
@aikimark

I have a source file, as attached (sample file.txt) that i am importing to excel to do some computation and processing.

After this, a file will be generated in excel. When i try to export this to a text file (results.txt), all the positioning from the source has disappeared. As this text file will be read by a host programme, based on positions, my host had inevitably returned error.

I have tried doing the processing using the text file directly using excel vb (without importing to excel), but the processing is just too complicated, hence it will be really important to retain my current processing bit in excel (i don't want to rewrite the whole processing part ;( )

I am looking for few lines or two to ensure that the resulted text file can be read by my host. The line above didn't work, as i could not get the desired output for my test result file.

Any help would be greatly appreciated
SAMPLE-FILE.txt
Results.txt
Looking at the Sample and Results text files, It appears that you are doing very little 'processing' other than changing some date strings.  The Results file only looks different because it is tab delimited, rather than the space-filled report you started with.

In order for us to provide a solution, you need to tell us what format and data your host expects.
No, the full processing is being done and generated in 4 different files.

The output files are currently very different from the source in positions. field 1 take pos 1 -2, field 2 takes position 3-5 and etc

The host expects same format as the source file (differences between the two are visible in wordpad)
Open the Sample and Results text files in Word, not Wordpad.  Click on the paragraph toolbar icon (view non printable characters).  Notice that the major difference between the two files is a replacement of the spaces with tab characters as 'field' separators.
Acceptable solution, but need to migrate