import data

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi!

I am making a macro that imports data and makes a list of the one I need.

I only need data in the first 5 lines. since it is a rather long file I
would like to only import the first lines. In the import dialog there is an
option that allows for a starting line.

How do I stop importing after 5 lines.

Jacob
 
You can either import the entire file and clear the unneeded data or use
VB's file i/o commands in a macro like this:

Sub FileImport()
Dim ResultStr As String
Dim FileName As String
Dim FileNum As Integer
Dim Counter As Double
FileName = InputBox("Name of text file to import:")
If FileName <> "" Then
FileNum = FreeFile()
Open FileName For Input As #FileNum
While Not EOF(FileNum) And Counter < 5
Counter = Counter + 1
Line Input #FileNum, ResultStr
Cells(Counter, 1).Value = ResultStr
Wend
Close
End If
End Sub

--
Jim Rech
Excel MVP
| Hi!
|
| I am making a macro that imports data and makes a list of the one I need.
|
| I only need data in the first 5 lines. since it is a rather long file I
| would like to only import the first lines. In the import dialog there is
an
| option that allows for a starting line.
|
| How do I stop importing after 5 lines.
|
| Jacob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top