Tab delimited Text into Excel using VBA


I am building a basic program that requires excel to read and import data from a tab delimited notepad file onto a worksheet. The notepad file has 5 columns with a sample line looking like this:

YM SELL 127 12600.25 1/2/2009

My code is below. I think I am either:
1.) Assinging the data types incorrectly
2.) Starting each parse from the 1st column (column 'A') of every line of the text file instead of column A, B, C, D, or E (columns 1-5 on the text file respectively) because it gives me an "over flow error."
OR 3.) Both

No one online seems to be able to answer this question, but I feel like I am making a stupid mistake that an expert would be able to notice.


Sub ReadTextFile()
Dim A As String * 2, B As String * 4, C As Integer, D As Single, E As Date
Dim iRow As Long
Dim Fname As Variant
Fname = Application.GetOpenFilename("Text Files (*.txt),*.txt", , _
"Select Text Data File")
If Fname = False Then Exit Sub

Open Fname For Input As #1
iRow = 1
Do While Not EOF(1)
Input #1, A, B, C, D, E
Cells(iRow, 1) = A
Cells(iRow, 1) = B
Cells(iRow, 1) = C
Cells(iRow, 1) = D
Cells(iRow, 1) = E
iRow = iRow + 1
Loop
Close 1
End Sub

I would greatly appreciate any insight you might have!

Thanks,
Dave


Comments

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion