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: 1
Default Change delimiter in import function from comma to tab


Gday all,

I have been using this code from Pearson
http://www.cpearson.com/excel/imptext.htm#Import and changed i
slightly (bold red).

However, i want to also use this file to import tab-delimited values
What is the delimiter character i need to stipulate for this?




Code
-------------------
Private Sub CommandButton1_Click()
Dim FName As Variant
Dim Sep As String

FName = Application.GetOpenFilename _
(filefilter:="CSV Files(*.csv),*.txt,All Files (*.*),*.*")
If FName = False Then
MsgBox "Error: You didn't select a CSV file to import, please run process again"
Exit Sub
End If

' SET DELIMITER AS COMMA
SEP = \",\


ImportTextFile CStr(FName), Sep

End Sub

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:

'Status bar indicating current process
Application.StatusBar = "Please wait: Data currently being imported"

' set A1 as active code, will eventually change coding to always place in cell A1
Range("A1").Select

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

-------------------

--
marksince198
-----------------------------------------------------------------------
marksince1984's Profile: http://www.excelforum.com/member.php...fo&userid=3348
View this thread: http://www.excelforum.com/showthread.php?threadid=55127

 
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
Excel: save-as csv : delimiter set to semi-colon, not comma. Why? Luke Skywalker Excel Discussion (Misc queries) 1 August 7th 06 02:18 PM
Comma delimiter Jon Quixley Excel Worksheet Functions 1 July 25th 06 04:47 PM
find and remove a string of a cell value with comma as delimiter yefei Excel Discussion (Misc queries) 3 February 28th 06 01:05 PM
Comma delimiter for csv downloads? Les M Excel Discussion (Misc queries) 7 January 31st 06 01:03 AM
CSV (Comma Separated Values) delimiter ? Lisa Pearlson Excel Programming 6 January 6th 04 02:30 PM


All times are GMT +1. The time now is 03:25 AM.

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"