2014-12-24 98 views
0

我打算在vba中編寫一個腳本,用於將多個文本文件導入到excel(一張),然後在一張圖上繪製它們。 我正面臨刷新BackgroundQuery問題並落在1004運行時錯誤。在Excel 2010中運行時錯誤1004刷新BackgroundQuery

我該如何解決?

感謝, 的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 

回答

1

它看起來像你試圖用一個帶引號的字符串中的路徑和文件名的變量。將變量連接到帶引號的字符串中。

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

這應該把變量的值放入字符串中,而不是它們的變量名稱。

+0

太棒了!謝謝。你知道我怎樣才能在同一個圖表上顯示所有導入的數據? – Eyal