将CSV文件导入Excel

我想请求您的帮助,如下所示:

我有从Excel中导入的软件应用程序导出的CSV文件来分析数据。 每日生成40-50个CSV。 现在我通过“从文本中获取外部数据”来手动完成此操作。 导入期间logging的代码是:

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文件,我将放置新文件并启动导入过程。 每个文件应该在前一个文件的最后一行之后立即插入。

您的帮助将不胜感激。

把你logging在一个函数中的代码,用一个variablesreplace静态文件名,然后为文件夹中的每个*.csv文件调用该函数。 得到下面的例子,你需要将这个macros的文件保存在与csv文件相同的文件夹中。 对于我的快速testing,我不得不更换分隔符;,并删除最后一行.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