【问题标题】:VBA: How to use InputBox to prompt user for .txt file path when importing .txt to Excel?VBA:将.txt导入Excel时如何使用InputBox提示用户输入.txt文件路径?
【发布时间】:2018-01-08 13:39:00
【问题描述】:

我一直在关注其他线程,该线程向我展示了如何使用 VBA 编辑器中的查询表将 .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 

我已尝试修改代码,以便不必每次都对路径进行硬编码,而是提示用户输入带有 InputBox 的路径,该 InputBox 将路径作为字符串存储在变量中,然后调用该变量而不是路径。

我不断收到有关 .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

感谢您的帮助,

谢谢

【问题讨论】:

    标签: excel import inputbox vba


    【解决方案1】:

    你需要改变

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

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

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

    【讨论】:

      【解决方案2】:

      你需要换行:

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

      与:

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

      VBA 中的双引号不会扩展变量(就像在 PowerShell 和 Perl 中一样);你必须明确地连接。

      【讨论】:

      • 谢谢你。这就像一个魅力。你的解释很有道理。
      【解决方案3】:

      更改
      Connection:="TEXT;textloc"

      Connection:="TEXT;" & textloc

      【讨论】:

        猜你喜欢
        • 2018-06-26
        • 2021-03-29
        • 2023-04-10
        • 1970-01-01
        • 2019-04-24
        • 1970-01-01
        • 2019-12-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多