Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Rob
 
Posts: n/a
Default how do I import tab delimited data?

Hi All, I am trying to do a macro that imports tab delimited data in two
columns. I started with a macro from here.
http://www.cpearson.com/excel/imptext.htm , and tweaked it slightly to my
needs, but it won't work for tab delimited data. I don't need to select the
delimiter, as it will always be tabbed. My code is below in two macros, can I
make this into one? Thanks in adnvance.

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

***********************

Public Sub DoTheImport()

Dim FName As Variant
Dim Sep As String

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

Sep = InputBox("Enter a single delimiter character.", _
"Import Text File")
ImportTextFile CStr(FName), Sep

End Sub
  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Kou Vang
 
Posts: n/a
Default how do I import tab delimited data?

I also have been working on importing text files, but I have something more
along this line for the importing:

Workbooks.OpenText filename:="DATA.TXT", _
dataType:=xlDelimited, tab:=True

What you have seems quite long, unless you need to do more things. If the
data is seperated by Tabs into two columns, I would think this would be
enough. HTH!

"Rob" wrote:

Hi All, I am trying to do a macro that imports tab delimited data in two
columns. I started with a macro from here.
http://www.cpearson.com/excel/imptext.htm , and tweaked it slightly to my
needs, but it won't work for tab delimited data. I don't need to select the
delimiter, as it will always be tabbed. My code is below in two macros, can I
make this into one? Thanks in adnvance.

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

***********************

Public Sub DoTheImport()

Dim FName As Variant
Dim Sep As String

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

Sep = InputBox("Enter a single delimiter character.", _
"Import Text File")
ImportTextFile CStr(FName), Sep

End Sub

Reply
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
How can I have the Import External Data dialog pop up on open? Benny Setting up and Configuration of Excel 1 November 19th 05 10:13 AM
Import Data Keeps asking for Password Dominator Excel Discussion (Misc queries) 0 June 5th 05 11:25 PM
Sort pages? David Excel Discussion (Misc queries) 15 May 13th 05 11:33 PM
import select data from another workbook schoeller Excel Worksheet Functions 0 March 31st 05 03:13 AM
Automatic Data Import TxRaistlin Excel Discussion (Misc queries) 2 February 4th 05 10:43 PM


All times are GMT +1. The time now is 11:46 AM.

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

About Us

"It's about Microsoft Excel"