-->

在Excel 2010中刷新BackgroundQuery运行时错误1004(run time er

2019-10-21 11:15发布

我特林写在VBA脚本导入几个文本文件到Excel(一张),比其画在一张图。 我对着在刷新BackgroundQuery commant一个问题,在1004运行时错误下降。

我如何可以解决这个问题?

谢谢,的Eyal

这里是我的代码:

Sub fring1()

    Dim fpath As String
    Dim fname As String
    Dim i As Integer

    fpath = "C:\Users\epinkas\Desktop\Yossi\"
    fname = fpath & "*.txt"

    Name = Dir(fname)
    While Name <> ""

        With Sheet1.QueryTables.Add(Connection:= _
          "TEXT;fpath & Name", _
          Destination:=Range("$A$1"))
            .Name = fpath & Name
            .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 = False
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
        ActiveSheet.Shapes.AddChart.Select
        ActiveChart.ChartType = xlXYScatterSmoothNoMarkers
        ActiveChart.SetSourceData Source:=Range("Sheet1!$A$1:$A$1356")

        Name = Dir()
    Wend

End Sub

Answer 1:

它看起来像你试图用一个带引号的字符串中的路径和文件名的变量。 将这些变量到引号的字符串。

    With Sheet1.QueryTables.Add(Connection:= _
      "TEXT;" & fpath & Name, _
      Destination:=Range("$A$1"))

这将会使变量的值到字符串,而不是他们的变量的名称。



文章来源: run time error 1004 in excel 2010 Refresh BackgroundQuery