LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Problem importing a txt file

I'm using Chip Pearson's routine to import a txt file into Excel using
VBA (see below). This works like a charm as long as the data in the
textfile start on the first row. Problem is that I first have a number
of lines in the textfile with some descriptive text in it. How would I
adapt Chip's code to start importing from a certain line number in the
txt file? What might be helpful is the fact that the last line before
the actual delimited data always starts with "item". I have attached a
sample for your review below. As ever, your help is much appreciated.

regards,
Jerry

Chip's code (taken from http://www.cpearson.com/excel/imptext.htm):

Public Sub ImportTextFile(FName As String, Sep As String)

Dim RowNdx As Integer
Dim ColNdx As Integer
Dim TempVal As Variant
Dim WholeLine As String
Dim Pos As Integer
Dim NextPos As Integer
Dim SaveColNdx As Integer

Application.ScreenUpdating = False
'On Error GoTo EndMacro:

SaveColNdx = ActiveCell.Column
RowNdx = ActiveCell.row

Open FName For Input Access Read As #1

While Not EOF(1)
Line Input #1, WholeLine
If Right(WholeLine, 1) < Sep Then
WholeLine = WholeLine & Sep
End If
ColNdx = SaveColNdx
Pos = 1
NextPos = InStr(Pos, WholeLine, Sep)
While NextPos = 1
TempVal = Mid(WholeLine, Pos, NextPos - Pos)
Cells(RowNdx, ColNdx).Value = TempVal
Pos = NextPos + 1
ColNdx = ColNdx + 1
NextPos = InStr(Pos, WholeLine, Sep)
Wend
RowNdx = RowNdx + 1
Wend

EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
Close #1

End Sub


------------Sample text file----------------

Report jan 2004
Generated by Jerry

Items_in_Report
01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49

Layer Translation
item price client
1 56 f.g.
2 87 t.y.
3 987 s.l.
etc.
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in importing text file! via135 Excel Discussion (Misc queries) 2 May 4th 07 06:37 AM
Importing Problem workerboy Excel Worksheet Functions 0 May 15th 06 01:45 PM
Importing text file, only option to edit existing file smokey99 Excel Discussion (Misc queries) 8 April 26th 06 09:08 PM
importing a text file - problem with dates "Americanising" strippier Excel Discussion (Misc queries) 3 February 21st 06 05:42 PM
Problem Importing a TXT File mike New Users to Excel 1 July 11th 05 10:13 PM


All times are GMT +1. The time now is 08:48 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"