run time error 1004 in excel 2010 Refresh BackgroundQuery

纵然是瞬间 提交于 2019-12-13 00:38:36

问题


I am tring to write a script in vba for importing several text files to excel (one sheet) and than draw them on one graph. I am facing a problem in Refresh BackgroundQuery commant and falls on 1004 run time error.

How can i work it out?

Thanks, Eyal

Here is my code:

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

回答1:


It looks like you are trying to use your path and filename variables inside a quoted string. Concatenate the variables into the quoted string.

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

That should put the values of the variables into the string, not their variables names.



来源:https://stackoverflow.com/questions/27635708/run-time-error-1004-in-excel-2010-refresh-backgroundquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!