私は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
、
は、変更する必要が
aucupariaありがとうございます。これは魅力的に機能しました。あなたの説明は理にかなっています。 –