Import from txt file.

tknig01

New Member
Joined
Jul 21, 2002
Messages
26
Hi, i'm trying to write a macro that obtains data from a huge database extract. The amount of data would easily exceed excel's capabillity so instead of a straightforward extract, i've dumped the info into a text file so i can import in chunks.

Split into groups using fields in col A (fields are either "alpha" or "beta") the data would fit on to a spreadsheet. Can anyone give me the code to only import rows where col A = alpha in a text file.

Thanks Tom Knight.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
You would need to read in the file one line at a time. Something like this (not tested):

Code:
Sub Test()
   Dim r As Long
   Dim Data As String
   Dim Rng As Range
   Set Rng = Range("A1")
   Open "C:TextFile.txt" For Input As #1
   r = 0
   Do While Not EOF(1)
      Line Input #1, Data
      If Left(Data,5) = "Alpha" Then
         Rng.Offset(r,0) = Data
         r = r + 1
      End If
   Loop
   Close #1
End Sub

Amend the target range and text file name to suit. The data will come in as a single column. You can use Text to Columns to parse it.
 

Forum statistics

Threads
1,147,498
Messages
5,741,504
Members
423,663
Latest member
kaveh87rsh

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