# Importing selected columns from a text file

#### Jon H

##### New Member
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

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

*bump*

#### jindon

##### MrExcel MVP
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
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
How many lines (records) do the txt file have?

Is it over 65536 ?

#### Jon H

##### New Member
No, it has just 13 rows of text.

#### jindon

##### MrExcel MVP
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
Brilliant, that works perfectly.

Thank you so much for your help Jindon.

Replies
5
Views
168
Replies
2
Views
147
Replies
7
Views
430
Replies
3
Views
389
Replies
3
Views
347

1,172,042
Messages
5,878,867
Members
433,380
Latest member

### 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.

### Which adblocker are you using?

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

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