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

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,485
Messages
6,113,931
Members
448,533
Latest member
thietbibeboiwasaco

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