Importing/Manipulating TAB seperated text file

captainx

New Member
Joined
Mar 15, 2011
Messages
8
Hope this is an easy one.

I have to import a pretty ugly text file on a regular basis, to make import much smoother I would like to read the file and attached lines 1, 2 and 3 together.

Then rinse and repeat.

This will stop us destroying the output file with columns and save a lot of hassle reformatting.

Can I use VB to Open, manipulate then Save As to keep the text file native before it gets into excel?
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Try this

Code:
    Dim Linevalues() As Variant
    Dim Counter As Long
    Dim Astr as string
    Counter = 0
    Dim FileName As String
    FileName = "C:\YourPath\YourFileName"
    Open FileName For Input As #1
    Do Until EOF(1) 'This simply counts number of lines in ASCII file
        Line Input #1, Astr
        Counter = Counter + 1
    Loop
    Close #1
    ReDim Linevalues(Counter)
    Counter = 0
    Open FileName For Input As #1
       Do Until EOF(1) 'Reads ASCII file line by line
          Line Input #1, Astr
          Linevalues(Counter) = Astr
          Counter = Counter + 1
       Loop
    Close #1
    Open FileName For Output As #1
        For i = 0 To Counter - 1
            Print #1, Linevalues(i)
        Next i
        
        'Your code here - what ever you want to add
        'Construct YourString
       
        Print #1, yourString
    Close #1
 
Upvote 0
This leaves the file as is - then you ad your values at the end - or did you want to combine the first 3 lines in ASCII file into a single string first - maybe I misunderstood

If so you can do this

Open file - do the loop EOF(1) - End Of File

Line Input #1,Astr
Line Input #1,AAstr
Line Input #1,AAAstr

NewString= Astr & VbTab & AAstr & VbTab & AAAstr

Now place this into your worksheet

This is old old DOS code - but it still works - just not efficient - but vey durable.
 
Upvote 0
If so you can do this

Open file - do the loop EOF(1) - End Of File

Line Input #1,Astr
Line Input #1,AAstr
Line Input #1,AAAstr

NewString= Astr & VbTab & AAstr & VbTab & AAAstr

That's gonna nail it, cheers.

I had just finished reading through the script and you have answered my question about joinging the lines.

Awesome, thanks.
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top