Option Explicit ' read CSV Sub Usage() Call fnReadCSV("C:\Users\david\Desktop\nw_products.txt") End Sub Public Function fnReadCSV(sFilename As String) On Error GoTo fnReadCSV_Error_Routine Dim sErrFilename As String sErrFilename = "c:\tmp\err" & Format(Now(), "yyyymmddHHMMSS") & ".txt" Dim i As Long Dim r As Long Dim c As Long Dim s As String Dim sInputChar As String Dim sComma As String i = 1 s = "" r = 1 c = 1 sInputChar = "" sComma = "," Open sFilename For Input As #10 Do Until EOF(10) sInputChar = Input(1, #10) If Asc(sInputChar) < 127 Then If sInputChar = sComma Then Cells(r, c) = s s = "" c = c + 1 GoTo Next_Char End If If sInputChar = vbCr Then ' MsgBox (s) ' MsgBox ("CR") ' Debug.Print "CR" Cells(r, c) = s s = "" c = 1 r = r + 1 GoTo Next_Char End If If sInputChar = vbLf Then ' MsgBox ("LF") ' Debug.Print "LF" GoTo Next_Char End If s = s & sInputChar End If Next_Char: i = i + 1 If i = 40000 Then Exit Do ' debug Loop fnReadCSV_Exit_Routine: ' MsgBox "Exit Routine" Close #10 Exit Function fnReadCSV_Error_Routine: Open sErrFilename For Output As #9 Print #9, "Error Log" Print #9, Now() Print #9, Err.Number Print #9, Err.Description Print #9, "fnReadCSV" Print #9, s Print #9, sInputChar Print #9, r Print #9, c Print #9, i Print #9, "**END**" Close #9 Resume fnReadCSV_Exit_Routine End_Function: End Function |