Importing selected columns from a text file

Jon H

New Member
Joined
Nov 23, 2005
Messages
17
Hi all,

I need to import a table from a text file into Excel. The text file has numbers that are delimited by semi-colons. The problem is that there are around 450 columns and excel obviously only allows 256 columns. Fortunately I don't need all of the 450 columns, just around 200 odd of them. So what I need to do is selectively import certain columns.

Each row of the text file is something like this:

2700; 1.8; 0.0; 0.00; 96; 1.8; 96; 13.4; 5.6; 1.00;.........

The first column is the category and the subsequent columns are the data. The columns I need to import are the:

1st (ie the category column), then the 2nd, 5th, 8th, 11th, 14th etc etc (ie only every third column).

Can anyone help me to import this data? Any help would be greatly appreciated!

I am running Excel 2003 with Service Pack 1.

Many thanks
Jon
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995
Hi
try this one
Code:
Sub test()
Dim myF As String, a(), ff As Integer, txt As String
Dim n As Long, t As Long, i As Long
myF = "C:\test.txt"  '<- need to alter
t = 1
ff = FreeFile
Open myF For Input As #ff
ReDim a(1 To Rows.Count, 1 To Columns.Count)
Do While Not EOF(ff)
     Line Input #ff, txt
     x = Split(txt,";")
     n = n + 1 : t = 2
     a(n,1) = x(0)
     For i = 1 To UBound(x) Step 3
          a(n,t) = x(i)
          t = t + 1
     Next
Loop
Close #ff
ThisWorkbook.Sheets(1).Range("a1").Resize(n,Columns.Count) = a
End Sub
 

Jon H

New Member
Joined
Nov 23, 2005
Messages
17
Hi Jindon,

Many thanks for your help on this. When I run your code I get the message "Run Time Error 9: Subscript out of range" and the following line is highlighted as the error: a(n, t) = x(i)

Any idea where I am going wrong at all?

Thanks again,
Jon
 

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995

ADVERTISEMENT

How many lines (records) do the txt file have?

Is it over 65536 ?
 

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995
How about
Code:
Sub test()
Dim myF As String, a(), ff As Integer, txt As String
Dim n As Long, t As Long, i As Long
myF = "C:\test.txt"  '<- need to alter
t = 1
ff = FreeFile
Open myF For Input As #ff
ReDim a(1 To Rows.Count, 1 To Columns.Count)
Do While Not EOF(ff)
     Line Input #ff, txt
     x = Split(txt,";")
     n = n + 1 : t = 2
     a(n,1) = x(0)
     For i = 1 To UBound(x) Step 3
          If i > UBound(x) Then Exit For
          a(n,t) = x(i)
          t = t + 1
     Next
Loop
Close #ff
ThisWorkbook.Sheets(1).Range("a1").Resize(n,Columns.Count) = a
End Sub
 

Jon H

New Member
Joined
Nov 23, 2005
Messages
17
Brilliant, that works perfectly.

Thank you so much for your help Jindon. :biggrin:
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,167,817
Messages
5,855,799
Members
431,764
Latest member
scottishbigyin

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
Top