2017-10-25 6 views
0

Excelにテキストファイルをインポートしようとすると問題が発生しました。私は以下のセミコロン区切り文字でインポートを完全に処理するVBAコードを書いています。他の区切り文字を使用したVBAテキストのインポート "="

;しかし

、私は(私の場合は等しい)他の区切り文字を含めるようにしようとも

=

それは私にエラーがスローされます。

オブジェクト 'Workbooks'の 'Open Text'メソッドが失敗しました。

通常

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 

提案.cfg拡張子を持つ添付私のサンプルテキストファイルを見つけてください、アイデアは、この問題を解決するのには本当に参考になります。

ありがとうございます。

+3

Other:=True, OtherChar="="に変更更新しました:= true'のセミコロン 'には:その他= false'をと':= false'をするに 'その他:= True、OtherChar = "=" ' – Tom

+0

@Tomそれは動作します。ありがとう – Agni

答えて

1

あなたは他の基準を含める必要があります。これにより、複数のデリミタを処理できます。

私はこのOther:=Falseあなた `Workbooks.OpenText`ラインの変更`セミコロンで

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 
+0

同じコードで複数のファイルを選択する方法 – Agni

+0

このコードで各ファイルをループする必要があります – Tom

関連する問題