VBA: 如何在将.txt 导入Excel 时使用InputBox 提示用户输入.txt 文件路径?

VBA: How to use InputBox to prompt user for .txt file path when importing .txt to Excel?

我一直在关注其他话题,其中向我展示了如何在 VBA 编辑器中使用查询表将 .txt 文件从特定路径导入到工作表中。

代码如下:

Sub Sample()
    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;C:\Sample.txt", Destination:=Range("$A") _
        )
        .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"))

        .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

感谢任何帮助,

谢谢

改变
Connection:="TEXT;textloc"

Connection:="TEXT;" & textloc

你需要改变

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

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

textloc 是一个变量,因此不能放在引号内。

您需要更换行:

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

与:

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

VBA 中的双引号不会扩展变量(就像您在 PowerShell 和 Perl 中所做的那样);你必须显式连接。