将CSV文件导入Excel

将CSV文件导入Excel,excel,vba,csv,import,Excel,Vba,Csv,Import,我想在以下方面寻求您的帮助: 我有从软件应用程序导出的CSV文件,需要导入Excel以分析数据。每天产生40-50个CSV。现在,我通过“从文本中获取外部数据”手动执行此操作。导入过程中记录的代码为: With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;SYSTEM:Users:catalin:Documents:LINELLA:WH Analytics:data:pick 01-18:050:Inquiry closed list

我想在以下方面寻求您的帮助:

我有从软件应用程序导出的CSV文件,需要导入Excel以分析数据。每天产生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文件,在该文件夹中我将放置新文件并启动导入过程。每个文件都应插入到前一个文件的最后一行之后


非常感谢您的帮助。

将录制的代码放入函数中,用变量替换静态文件名,然后为文件夹中的每个
*.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
Sub-ImportAllCSV()
Dim FName作为变型,R作为长型
R=1
FName=Dir(“*.csv”)
当FName“”时执行
ImportCsvFile FName,ActiveSheet.Cells(R,1)
R=ActiveSheet.UsedRange.Rows.Count+1
FName=Dir
环
端接头
子导入文件(文件名作为变量,位置作为范围)
使用ActiveSheet.QueryTables.Add(连接:=_
“TEXT;”文件名(&F)_
,目的地:=位置)
.Name=Replace(文件名“.csv”,”)
.FieldNames=True
.rowNumber=False
.FillAdjacentFormulas=False
.refreshinfoleopen=False
.BackgroundQuery=True
.RefreshStyle=xlInsertDeleteCells
.SavePassword=False
.SaveData=True
.AdjustColumnWidth=True
.TextFilePromptOnRefresh=False
.TextFilePlatform=xlMacintosh
.TextFileStartRow=1
.TextFileParseType=xlDelimited
.TextFileTextQualifier=xlTextQualifierDoubleQuote
.textfileconsutivedelimiter=False
.TextFileTabDelimiter=True
.TextFileSemicolonDelimiter=False
.textfilecommadelimitor=False
.TextFileSpaceDelimiter=False
.TextFileOtherDelimiter=“,”
.TextFileColumnDataTypes=数组(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)
.Refresh BackgroundQuery:=False
以
端接头
可能有用吗?您仍然需要手动选择最后一行(CTRL+Down),但其余行将自动选择。