Solved

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

Posted on 2011-02-16
14
410 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
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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
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.

707 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now