Import CSV files into Excel

前端 未结 1 1864
后悔当初
后悔当初 2020-12-14 11:56

I would like to ask for your help with the following:

I have CSV files exported from a software application that I need imported in Excel to analyse the data. Daily

相关标签:
1条回答
  • 2020-12-14 12:48

    Put the code you recorded in a function, replacing the static file name with a variable, then call that function for each *.csv file in the folder. The get the example below to work you need to save a file with this macro in the same folder as the csv files. For my quick test I had to replace the separator from ; to ,, and to remove the last row .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
    
    0 讨论(0)
提交回复
热议问题