VBA 使用其他分隔符“=”导入文本
VBA text import with other delimiter "="
我在尝试将文本文件导入 Excel 时遇到问题。我写了一个 VBA 代码,它完美地处理了带有以下分号分隔符的导入。
;
但是,当我尝试包含其他分隔符(在我的例子中等于)时
=
它抛出一个错误。
The method ' Open Text' for the object 'Workbooks' failed.
请查看附件中我的示例文本文件,该文件的扩展名通常为 .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;
及其对应的VBA代码
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
建议、想法对解决这个问题很有帮助。
提前致谢。
您只需要包括您的其他条件。这可以处理多个定界符。
我已将此更改 Other:=False
更新为 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
我在尝试将文本文件导入 Excel 时遇到问题。我写了一个 VBA 代码,它完美地处理了带有以下分号分隔符的导入。
;
但是,当我尝试包含其他分隔符(在我的例子中等于)时
=
它抛出一个错误。
The method ' Open Text' for the object 'Workbooks' failed.
请查看附件中我的示例文本文件,该文件的扩展名通常为 .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;
及其对应的VBA代码
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
建议、想法对解决这个问题很有帮助。
提前致谢。
您只需要包括您的其他条件。这可以处理多个定界符。
我已将此更改 Other:=False
更新为 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