run time error 1004 in excel 2010 Refresh Backgrou

2019-08-07 08:35发布

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条回答
太酷不给撩
2楼-- · 2019-08-07 09:32

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.

查看更多
登录 后发表回答