Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 429
  • Last Modified:

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
0
albasatar
Asked:
albasatar
  • 6
  • 6
1 Solution
 
albasatarAuthor Commented:
Note that this has to be a vba solution as i have big files to process
0
 
danaseamanCommented:
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
 
aikimarkCommented:
@albasatar

The example you posted is not in a fixed length field format.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
albasatarAuthor Commented:
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
 
aikimarkCommented:
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
 
albasatarAuthor Commented:
Ya, apologies, it is space-delimited rather than fixed length.

The code above does not work, unfortunately.
0
 
aikimarkCommented:
@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
 
aikimarkCommented:
@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
 
albasatarAuthor Commented:
@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
 
aikimarkCommented:
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
 
albasatarAuthor Commented:
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
 
aikimarkCommented:
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
 
albasatarAuthor Commented:
Acceptable solution, but need to migrate
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now