2013-07-27 221 views
7

我想問您的幫助有以下幾點:導入CSV文件導入Excel

我有我需要進口在Excel中分析數據的軟件應用程序中導出CSV文件。每日生成40-50個CSV。現在我通過「從文本獲取外部數據」手動完成此操作。在導入過程中記錄的代碼是:

With ActiveSheet.QueryTables.Add(Connection:= _ 
    "TEXT;SYSTEM:Users:catalin:Documents:LINELLA:WH Analytics:data:pick 01-18:050:Inquiry closed lists SKU_0142.csv" _ 
    , Destination:=Range("A1704")) 
    .Name = "Inquiry closed lists SKU_0142" 
    .FieldNames = True 
    .RowNumbers = False 
    .FillAdjacentFormulas = False 
    .RefreshOnFileOpen = False 
    .BackgroundQuery = True 
    .RefreshStyle = xlInsertDeleteCells 
    .SavePassword = False 
    .SaveData = True 
    .AdjustColumnWidth = True 
    .TextFilePromptOnRefresh = False 
    .TextFilePlatform = xlMacintosh 
    .TextFileStartRow = 1 
    .TextFileParseType = xlDelimited 
    .TextFileTextQualifier = xlTextQualifierDoubleQuote 
    .TextFileConsecutiveDelimiter = False 
    .TextFileTabDelimiter = True 
    .TextFileSemicolonDelimiter = False 
    .TextFileCommaDelimiter = False 
    .TextFileSpaceDelimiter = False 
    .TextFileOtherDelimiter = ";" 
    .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) 
    .Refresh BackgroundQuery:=False 
    .UseListObject = False 
End With 
Selection.End(xlDown).Select 
Range("A1710").Select 

我希望能夠從選定的文件夾,我會把新文件並啓動導入過程中自動導入所有CSV文件。每個文件應該在前一個文件的最後一行之後立即插入。

您的幫助將不勝感激。

+0

也許[這個插件會](http://superuser.com/questions/307496/how-can-i-set-excel-to-always-import-all-columns-of-csv -files-as-text/527894#527894)有用嗎?您仍然需要手動選擇最後一行(CTRL +向下),但其餘的將自動完成。 – nixda

回答

8

將您記錄在代碼中的代碼放在一個函數中,用一個變量替換靜態文件名,然後爲該文件夾中的每個*.csv文件調用該函數。得到下面的例子,你需要將這個宏的文件保存在與csv文件相同的文件夾中。對於我的快速測試,我必須將;的分隔符替換爲,,並刪除最後一行.UseListObject = False

Sub ImportAllCSV() 
    Dim FName As Variant, R As Long 
    R = 1 
    FName = Dir("*.csv") 
    Do While FName <> "" 
    ImportCsvFile FName, ActiveSheet.Cells(R, 1) 
    R = ActiveSheet.UsedRange.Rows.Count + 1 
    FName = Dir 
    Loop 
End Sub 

Sub ImportCsvFile(FileName As Variant, Position As Range) 
    With ActiveSheet.QueryTables.Add(Connection:= _ 
     "TEXT;" & FileName _ 
     , Destination:=Position) 
     .Name = Replace(FileName, ".csv", "") 
     .FieldNames = True 
     .RowNumbers = False 
     .FillAdjacentFormulas = False 
     .RefreshOnFileOpen = False 
     .BackgroundQuery = True 
     .RefreshStyle = xlInsertDeleteCells 
     .SavePassword = False 
     .SaveData = True 
     .AdjustColumnWidth = True 
     .TextFilePromptOnRefresh = False 
     .TextFilePlatform = xlMacintosh 
     .TextFileStartRow = 1 
     .TextFileParseType = xlDelimited 
     .TextFileTextQualifier = xlTextQualifierDoubleQuote 
     .TextFileConsecutiveDelimiter = False 
     .TextFileTabDelimiter = True 
     .TextFileSemicolonDelimiter = False 
     .TextFileCommaDelimiter = False 
     .TextFileSpaceDelimiter = False 
     .TextFileOtherDelimiter = "," 
     .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) 
     .Refresh BackgroundQuery:=False 
    End With 
End Sub