close

Hi all, I am trying to write a macro that imports tab delimited data in two
columns. I am not that savvy with writing macros, so I copied the code from a
website. www.cpearson.com/excel/imptext.htm but I need to tweak it so
that it can import tab delimited data, I don't need to select the delimiter,
it will always be tabbed in this case. Here's what I have (2 macros), can I
do this in one?

Public Sub DoTheImport()

Dim FName As Variant
Dim Sep As String

FName = Application.GetOpenFilename _
(filefilter:=quot;Text Files(*.dat),*.dat,All Files (*.*),*.*quot;)
If FName = False Then
MsgBox quot;You didn't select a filequot;
Exit Sub
End If

Sep = InputBox(quot;Enter a single delimiter character.quot;, _
quot;Import Text Filequot;)
ImportTextFile CStr(FName), Sep

End Sub
****************************************
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) lt;gt; Sep Then
WholeLine = WholeLine amp; Sep
End If
ColNdx = SaveColNdx
Pos = 1
NextPos = InStr(Pos, WholeLine, Sep)
While NextPos gt;= 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

Thanks in advance. Rob

全站熱搜
創作者介紹
創作者 software 的頭像
software

software

software 發表在 痞客邦 留言(0) 人氣()