简体   繁体   English

VBA文本导入和其他定界符“ =”

[英]VBA text import with other delimiter “=”

I have encountered a problem when trying to import a text file into Excel. 尝试将文本文件导入Excel时遇到问题。 I wrote a VBA code which perfectly handles the import with the following semicolon delimiter. 我编写了一个VBA代码,该代码可以使用以下分号分隔符完美地处理导入。

; ;

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 请找到附件我的示例文本文件,该文件通常具有扩展名.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 及其对应的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

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="=" 我已经更新了此更改,将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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM