运行时错误1004在Excel 2010中刷新BackgroundQuery

我很想写一个脚本在vba中导入几个文本文件到excel(一张),然后在一张图上绘制它们。 我在刷新BackgroundQuery命令中遇到问题,并在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 

看起来你正在试图在引用string中使用你的path和文件名variables。 将variables连接到引用的string中。

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

这应该把variables的值放入string中,而不是它们的variables名称。