lucpian
04-02-2008, 12:44 PM
Hi All,
I am having a big problem with the code I wrote to import text files into Excel. It works fine, but as part of the requirement it should validate that the following column headings are there, and appears in the following order: First_Name, Last_Name, Purch_Ord_Num, Amount, Purc_Date, Received_By, Verified_By.
Here is the code,
Sub ImportTextFile()
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
Dim FileName As Variant
Dim Sep As String
Application.ScreenUpdating = False
On Error GoTo EndMacro:
lFNum = FreeFile
sFile = Application.GetOpenFilename(FileFilter:="Excel File (*.txt),*.txt")
'If sFile = False Then
''''''''''''''''''''''''''
' user cancelled, get out
''''''''''''''''''''''''''
' Exit Sub
'End If
Const sDELIM = "," 'Set the delimeter
lFNum = FreeFile
'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
EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
Close lFNum
End Sub
I have tried writing codes to add to it to do it but it is not working. I would be very grateful if someone in this forum will help me out.
Thanks
Lucpian
Edit Lucas: VBA Tags added: Lucipian, when posting your code select it and hit the VBA button...
I am having a big problem with the code I wrote to import text files into Excel. It works fine, but as part of the requirement it should validate that the following column headings are there, and appears in the following order: First_Name, Last_Name, Purch_Ord_Num, Amount, Purc_Date, Received_By, Verified_By.
Here is the code,
Sub ImportTextFile()
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
Dim FileName As Variant
Dim Sep As String
Application.ScreenUpdating = False
On Error GoTo EndMacro:
lFNum = FreeFile
sFile = Application.GetOpenFilename(FileFilter:="Excel File (*.txt),*.txt")
'If sFile = False Then
''''''''''''''''''''''''''
' user cancelled, get out
''''''''''''''''''''''''''
' Exit Sub
'End If
Const sDELIM = "," 'Set the delimeter
lFNum = FreeFile
'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
EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
Close lFNum
End Sub
I have tried writing codes to add to it to do it but it is not working. I would be very grateful if someone in this forum will help me out.
Thanks
Lucpian
Edit Lucas: VBA Tags added: Lucipian, when posting your code select it and hit the VBA button...