I have encountered a problem when trying to import a text file into Excel. I wrote a VBA code which perfectly handles the import with the following semicolon delimiter.
;
However, when I try to include other delimiter (In my case equal to) also
=
It throws me an error.
The method ' Open Text' for the object 'Workbooks' failed.
Please find attached my sample text file which usually has an extension .cfg
Dos-001-Zykl_Date_r(V1.0)=1401174131;27.05.2014 07:02:11;
Dos-002-Zykl_Date_r(V1.0)=1401174225;27.05.2014 07:03:45;
and its corresponding VBA code
Sub ImportTextFile()
'Imports a text file
Dim vFileName
On Error GoTo ErrorHandle
vFileName = Application.GetOpenFilename()
'If the user pressed "Cancel" or didn't select a text file,
'exit the procedure.
If vFileName = False Then
GoTo BeforeExit
End If
'Switch off screen updating for speed.
Application.ScreenUpdating = False
'We now import the selected text file, and data is
'inserted in a new spreadsheet. If you want to use
'another delimiter, you must change "Semicolon:=True"
'to "Semicolon:=False" and set another delimiter
'(e.g. "Tab") to True.
Workbooks.OpenText Filename:=vFileName, _
Origin:=xlMSDOS, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=True, _
Comma:=False, Space:=False, Other:=False, _
TrailingMinusNumbers:=True, Local:=True
'Just to show how we auto adjust the width of column A.
Columns("A:A").EntireColumn.AutoFit
BeforeExit:
Application.ScreenUpdating = True
Exit Sub
ErrorHandle:
MsgBox Err.Description
Resume BeforeExit
End Sub
Suggestions, ideas would be really helpful in solving this issue.
Thank you in advance.
You just need to include your other criteria. This can handle multiple delimiters.
I've updated this changing Other:=False
to Other:=True, OtherChar="="
Sub ImportTextFile()
'Imports a text file
Dim vFileName
On Error GoTo ErrorHandle
vFileName = Application.GetOpenFilename()
'If the user pressed "Cancel" or didn't select a text file,
'exit the procedure.
If vFileName = False Then
GoTo BeforeExit
End If
'Switch off screen updating for speed.
Application.ScreenUpdating = False
'We now import the selected text file, and data is
'inserted in a new spreadsheet. If you want to use
'another delimiter, you must change "Semicolon:=True"
'to "Semicolon:=False" and set another delimiter
'(e.g. "Tab") to True.
Workbooks.OpenText fileName:=vFileName, _
Origin:=xlMSDOS, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=True, _
Comma:=False, Space:=False, Other:=True, OtherChar:="=", _
TrailingMinusNumbers:=True, Local:=True
'Just to show how we auto adjust the width of column A.
Columns("A:A").EntireColumn.AutoFit
BeforeExit:
Application.ScreenUpdating = True
Exit Sub
ErrorHandle:
MsgBox Err.Description
Resume BeforeExit
End Sub