J
Jerry
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.
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.