2016-02-18 56 views
0

我有一個導入.csv文件到工作表「數據」導入多個CSV文件導入到主Excel表格中的數據追加到以前的進口

首次導入後的腳本以下所有的人出現的正確的以前的進口並沒有添加到底部(最後一行)。

我認爲這個問題涉及到這方面的腳本: 目的地:= ThisWorkbook.Sheets( 「數據」)範圍( 「$ A $ 1」))

Sub load_csv() 
    Dim fStr As String 

    With Application.FileDialog(msoFileDialogFilePicker) 
     .Show 
     If .SelectedItems.Count = 0 Then 
      MsgBox "Cancel Selected" 
      Exit Sub 
     End If 
     'fStr is the file path and name of the file you selected. 
     fStr = .SelectedItems(1) 
    End With 

    With ThisWorkbook.Sheets("Data").QueryTables.Add(Connection:= _ 
    "TEXT;" & fStr, Destination:=ThisWorkbook.Sheets("Data").Range("$A$1")) 
     .Name = "CAPTURE" 
     .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 = True 
     .TextFileSpaceDelimiter = False 
     .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) 
     .TextFileTrailingMinusNumbers = True 
     .Refresh BackgroundQuery:=False 

    End With 
End Sub 
+0

你看過本網站上已解決此問題的答案嗎? – Jeeped

回答

0

您需要檢查目標看看下一個未使用的單元是什麼。

With ThisWorkbook.Sheets("Data") 
    .QueryTables.Add(Connection:= _ 
    ..., Destination:=.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) 
'lots of other stuff 
End With 

可以rereference反覆ThisWorkbook.Sheets( 「數據」),但使用With ... End With statement更容易。