# 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

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
*bump*

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``````

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

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

Is it over 65536 ?

No, it has just 13 rows of text.

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``````

Brilliant, that works perfectly.

Thank you so much for your help Jindon.

Replies
3
Views
265
Replies
5
Views
205
Replies
3
Views
698
Replies
5
Views
316
Replies
1
Views
346

1,219,100
Messages
6,146,282
Members
450,684
Latest member
wilto1tg

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