2017-08-01 8 views
1

私はVBAエディタでクエリテーブルを使用して特定のパスから.txtファイルをワークシートにインポートする方法を教えてくれるスレッドに従っています。代わり経路たびにハードコーディングするのではなく、ユーザが格納InputBox関数のパスの入力を要求するように、私はコードを変更しようとしたVBA:.txtをExcelにインポートするときにInputBoxを使用してユーザーに.txtファイルパスを入力する方法

Sub Sample() 
    With ActiveSheet.QueryTables.Add(Connection:= _ 
     "TEXT;C:\Sample.txt", Destination:=Range("$A$1") _ 
     ) 
     .Name = "Sample" 
     .FieldNames = True 
     .RowNumbers = False 
     .FillAdjacentFormulas = False 
     .PreserveFormatting = True 
     .RefreshOnFileOpen = False 
     .RefreshStyle = xlInsertDeleteCells 
     .SavePassword = False 
     .SaveData = True 
     .AdjustColumnWidth = True 
     .RefreshPeriod = 0 
     .TextFilePromptOnRefresh = False 
     .TextFilePlatform = 437 
     .TextFileStartRow = 1 
     .TextFileParseType = xlDelimited 
     .TextFileTextQualifier = xlTextQualifierDoubleQuote 
     .TextFileConsecutiveDelimiter = False 
     .TextFileTabDelimiter = True 
     .TextFileSemicolonDelimiter = False 
     .TextFileCommaDelimiter = True 
     .TextFileSpaceDelimiter = False 
     .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1) 
     .TextFileTrailingMinusNumbers = True 
     .Refresh BackgroundQuery:=False 
    End With 
End Sub 

次のよう

コードでありますパスを変数の文字列として指定すると、パスの代わりにその変数が呼び出されます。

.Refresh BackgroundQuery:= False行に関するエラーが発生し続けます。

以下は、私が変更したコードです。すべてのヘルプは高く評価され

Option Explicit 
Sub importEXP() 

Dim txtloc As String 

txtloc = InputBox("Provide path of .txt file to analyze") 

    With ActiveSheet.QueryTables.Add(Connection:="TEXT;textloc", destination:=Range("$A$1")) 

     .Name = "Sample" 
     .FieldNames = True 
     .RowNumbers = False 
     .FillAdjacentFormulas = False 
     .PreserveFormatting = True 
     .RefreshOnFileOpen = False 
     .RefreshStyle = xlInsertDeleteCells 
     .SavePassword = False 
     .SaveData = True 
     .AdjustColumnWidth = True 
     .RefreshPeriod = 0 
     .TextFilePromptOnRefresh = False 
     .TextFilePlatform = 437 
     .TextFileStartRow = 1 
     .TextFileParseType = xlDelimited 
     .TextFileTextQualifier = xlTextQualifierDoubleQuote 
     .TextFileConsecutiveDelimiter = False 
     .TextFileTabDelimiter = True 
     .TextFileSemicolonDelimiter = False 
     .TextFileCommaDelimiter = True 
     .TextFileSpaceDelimiter = False 
     .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1) 
     .TextFileTrailingMinusNumbers = True 
     .Refresh BackgroundQuery:=False 
    End With 

End Sub 

は、変更する必要が

答えて

0

VBAで二重引用符は、(あなたはPowerShellとPerlで行うことができますように)変数を展開しません

With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & textloc, destination:=Range("$A$1")) 

。明示的に連結する必要があります。

+0

aucupariaありがとうございます。これは魅力的に機能しました。あなたの説明は理にかなっています。 –

1

変更
Connection:="TEXT;textloc"
Connection:="TEXT;" & textloc

0


にありがとう

それは、引用符の内側に配置されてはならないので、

With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & textloc, destination:=Range("$A$1")) 

textlocから3210

は可変です。で

With ActiveSheet.QueryTables.Add(Connection:="TEXT;textloc", destination:=Range("$A$1")) 

:あなたが行を交換する必要があり

関連する問題