2013-07-27 20 views
7

chciałbym prosić o pomoc z następujących powodów:Import plików CSV do programu Excel

Mam plików CSV eksportowanych z aplikacji, które muszę importowanym w programie Excel do analizy danych. Dziennie generowane są 40-50 CSV. Na razie robię to ręcznie poprzez "Pobierz dane zewnętrzne z tekstu". Kod nagrany podczas importu jest:

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 

Chcę móc automatycznie zaimportować wszystkie pliki CSV z wybranego folderu, w którym włożę nowe pliki i uruchomić proces importowania. Każdy plik powinien zostać wstawiony natychmiast po ostatnim wierszu poprzednich plików.

Twoja pomoc będzie bardzo cenna.

+0

Może [ten dodatek będzie] (http://superuser.com/questions/307496/how-can-i-set-excel-to-always-import-all-columns-of-csv -files-as-text/527894 # 527894) Być przydatne? Nadal musisz wybrać ostatni wiersz ręcznie (CTRL + Dół), ale reszta będzie automatycznie. – nixda

Odpowiedz

8

Wpisz kod, który nagrałeś w funkcji, zastępując statyczną nazwę pliku zmienną, a następnie wywołaj tę funkcję dla każdego pliku *.csv w folderze. Poniższy przykład działa, aby zapisać plik z tym makrem w tym samym folderze, co pliki csv. Do mojego szybkiego testu musiałem wymienić separator z ; na , i usunąć ostatni wiersz .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 
Powiązane problemy