?
Solved

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

Posted on 2011-02-16
14
Medium Priority
?
424 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
  • 6
  • 6
13 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 1500 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 46

Expert Comment

by:aikimark
ID: 34917342
@albasatar

The example you posted is not in a fixed length field format.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 46

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 46

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 46

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 46

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 46

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

621 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