PDA

View Full Version : Importing a text file into an excel template



lucpian
03-14-2008, 08:25 AM
Hi All,

I wrote the following vba code as part of an automated process to import text file into an excel template, but I keep having errors whenever I run it. I have tried to fix this error, but it still does not work. Here, is the code: Sub GetTextFile()

Dim sFile As String
Dim sInput As String
Dim lFNum As Long
Dim vaFields As Variant
Dim i As Long
Dim lRow As Long
Dim vaStrip As Variant

Const sDELIM = "^" 'Set the delimeter

lFNum = FreeFile
sFile = Application.GetOpenFilename(FileFilter:="Text File (*.txt),*.txt")
If sFile = False Then
''''''''''''''''''''''''''
' user cancelled, get out
''''''''''''''''''''''''''
Exit Sub
End If
'sFile = "C:CaratDelim.txt"
vaStrip = Array(vbLf, vbTab) 'list the text to strip

'Open the file
Open sFile For Input As lFNum

'Loop through the file until the end
Do While Not EOF(lFNum)
Line Input #lFNum, sInput 'input the current line

'remove the unwanted text
For i = LBound(vaStrip) To UBound(vaStrip)
sInput = Replace(sInput, vaStrip(i), "")
Next i

'split the text based on the delimeter
vaFields = Split(sInput, sDELIM)
lRow = lRow + 1

'Write to the worksheet
For i = 0 To UBound(vaFields)
Sheet1.Cells(lRow, i + 1).Value = vaFields(i)
Next i
Loop

Close lFNum

End Sub

Please, I would be grateful if anyone in this forum can help me out.

Thanks

Lucpian

Aussiebear: Edited thread to enclose code within VBA Tags

OTWarrior
03-14-2008, 08:27 AM
What is the error?

lucpian
03-14-2008, 08:38 AM
I have this error "Run-time error '13' Type mismatch".


Thanks

Lucpian

kbsudhir
03-28-2008, 02:39 PM
Change "sFile = False" to sFile = "False"

It will work file and you will not get any error

mdmackillop
03-28-2008, 03:02 PM
Hi Lucpian,
Please use the VBA button to format your posted code.

Bob Phillips
03-28-2008, 04:23 PM
Change "sFile = False" to sFile = "False"

It will work file and you will not get any error

I would suggest that he changes the variable type



Dim sFile As Variant