Hello all, thank you in advance for any assistance.
I have special "csv " files containing specific delimiters (CHR (20) for the column separator and the thorn character chr(254) for the text qualifier)
I found the code below which i have modified slightly and gets me half of the way where i need to go. during the import process I have been able to specify the column separator when importing the file and now i need to remove the text qualifier(chr (254)) during the import process. I added the replace line
WholeLine = Replace(WholeLine, Chr(254), "", vbTextCompare)
below which does the trick, however i have not tired this with large files and this may cause a problem since the character is being deleted and not being used as a "Qualifier "
is there any method of accomplishing this in the code below? thank you in advance again.
I have special "csv " files containing specific delimiters (CHR (20) for the column separator and the thorn character chr(254) for the text qualifier)
I found the code below which i have modified slightly and gets me half of the way where i need to go. during the import process I have been able to specify the column separator when importing the file and now i need to remove the text qualifier(chr (254)) during the import process. I added the replace line
WholeLine = Replace(WholeLine, Chr(254), "", vbTextCompare)
below which does the trick, however i have not tired this with large files and this may cause a problem since the character is being deleted and not being used as a "Qualifier "
is there any method of accomplishing this in the code below? thank you in advance again.
VBA Code:
Public Sub ImportTextFile()
'Public Sub ImportTextFile(FName As String, Sep As String)
Dim Sep As String
Dim FName As String
Dim RowNdx As Long
Dim ColNdx As Integer
Dim TempVal As Variant
Dim WholeLine As String
Dim Pos As Integer
Dim NextPos As Integer
Dim SaveColNdx As Integer
Sep = Chr(20)
FName = "C:\Temp\enron.dat"
'disable screen updates
Application.ScreenUpdating = False
'error handling
On Error GoTo EndMacro
'Importing data starts from the selected cell in a worksheet
SaveColNdx = ActiveCell.Column
RowNdx = ActiveCell.Row
'open the file in read mode
Open FName For Input Access Read As #1
'Read the file until End of Line
While Not EOF(1)
'read line by line
Line Input #1, WholeLine
WholeLine = Replace(WholeLine, "", "", vbTextCompare) 'UTF-8
WholeLine = Replace(WholeLine, "ÿþ", "", vbTextCompare) 'UTF-16 Unicode little endian
WholeLine = Replace(WholeLine, "þÿ", "", vbTextCompare) 'UTF-16 Unicode big endian
[B] WholeLine = Replace(WholeLine, Chr(254), "", vbTextCompare)[/B]
'checking if the line is empty
If Right(WholeLine, 1) <> Sep Then
WholeLine = WholeLine & Sep
End If
ColNdx = SaveColNdx
Pos = 1
NextPos = InStr(Pos, WholeLine, Sep)
'finding each column data
While NextPos >= 1
TempVal = Mid(WholeLine, Pos, NextPos - Pos)
Cells(RowNdx, ColNdx).Value = TempVal
Pos = NextPos + 1
ColNdx = ColNdx + 1
NextPos = InStr(Pos, WholeLine, Sep)
Wend
RowNdx = RowNdx + 1
Wend
Close #1
Exit Sub