Excel VBA to read delimited text file

peter2047

Board Regular
Joined
Dec 31, 2005
Messages
70
Hi All,

I need to read a delimited text file in the following format.
03/30/2007,11:14:16,56.1,1000,abcdefgh,abc

I try this but this but having error at line input

Sub open_read()

Dim i, Filenum As Integer
Dim var1 as Date
Dim var2 as String
Dim var3 as Double
Dim var4 as Long
Dim var5 as String
Dim var6 as String

Dim current_path As String

current_path = ThisWorkbook.Path
Filenum = FreeFile
current_path = current_path & "\testcase.csv"

Open current_path For Input As #Filenum

For i = 1 To 1000
Line Input #Filenum, var1, var2, var3, var4, var5, var6
tick_array(0, 1, 2, 3, 4) = Split(tick, ",")
Sheets("Sheet1").Range("A1").Offset(i, 0) = var1
Sheets("Sheet1").Range("A1").Offset(i, 1) = var2
Sheets("Sheet1").Range("A1").Offset(i, 2) = var3
Sheets("Sheet1").Range("A1").Offset(i, 3) = var4
Sheets("Sheet1").Range("A1").Offset(i, 4) = var5
Next i

Close Filenum

End Sub

Thanks in advance for your.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Code:
Sub open_read()

Dim i, Filenum As Integer
dim txt as string, x

Dim current_path As String

current_path = ThisWorkbook.Path
Filenum = FreeFile
current_path = current_path & "\testcase.csv"

Open current_path For Input As #Filenum

For i = 1 To 1000
    Line Input #Filenum, txt
    x = Split(txt, ",")
    Sheets("Sheet1").Range("A" & i).Resize(,UBound(x) + 1).Value = x
Next i

Close Filenum

End Sub
 
Upvote 0

Forum statistics

Threads
1,222,629
Messages
6,167,190
Members
452,104
Latest member
jadethejade

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top