Solved

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

Posted on 2011-02-16
14
414 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:albasatar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
14 Comments
 

Author Comment

by:albasatar
ID: 34908784
Note that this has to be a vba solution as i have big files to process
0
 
LVL 22

Accepted Solution

by:
danaseaman earned 500 total points
ID: 34914117
This Vb6 solution should be almost identical in VBA:


Option Explicit

Private Sub Form_Load()
   Dim FF               As Integer
   Dim F2               As Integer
   Dim sLine            As String
   Dim sOut             As String
   Dim sFileOut         As String
   Dim vSplit()         As String
   Dim UB               As Long
   Dim i                As Long

   On Error Resume Next

   FF = FreeFile
   Open App.Path & "\myData.txt" For Input As FF
   sFileOut = App.Path & "\myNewData.txt"
   Kill sFileOut
   F2 = FreeFile
   Open sFileOut For Output As F2
   Do While Not EOF(FF)
      Line Input #FF, sLine
      If InStr(1, sLine, " ") Then
         vSplit = Split(sLine, " ")
         UB = UBound(vSplit)
         Select Case UB
            Case 2, 3 '3,4 Fields
               sOut = vSplit(0) & " "
               For i = 1 To UB
                  sOut = sOut & StrReverse(vSplit(i))
                  If i < UB Then
                     sOut = sOut & " "
                  End If
               Next
               'Debug.Print sLine 'Original
               'Debug.Print sOut  'Modified
               Print #F2, sOut   'Print to new file
         End Select
      End If
   Loop
   Close FF
   Close F2
End Sub

Open in new window

0
 
LVL 45

Expert Comment

by:aikimark
ID: 34917342
@albasatar

The example you posted is not in a fixed length field format.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:albasatar
ID: 34917549
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)

0
 
LVL 45

Expert Comment

by:aikimark
ID: 34917624
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

0
 

Author Comment

by:albasatar
ID: 34961320
Ya, apologies, it is space-delimited rather than fixed length.

The code above does not work, unfortunately.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 34961758
@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"?
0
 
LVL 45

Expert Comment

by:aikimark
ID: 34961797
@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.
0
 

Author Comment

by:albasatar
ID: 34981065
@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
0
 
LVL 45

Expert Comment

by:aikimark
ID: 34981599
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.
0
 

Author Comment

by:albasatar
ID: 34990662
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)
0
 
LVL 45

Expert Comment

by:aikimark
ID: 34991507
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.
0
 

Author Closing Comment

by:albasatar
ID: 35261086
Acceptable solution, but need to migrate
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question