多个CSV转换成一个Excel

遇到一个问题,很多CSV文件要合并为一个excel文件,手工操作基本没问题,不过费时费力,容易出错,找了一下,有VBA可以直接搞定,和大家一起分享。

遇到一个问题,很多CSV文件要合并为一个excel文件,手工操作基本没问题,不过费时费力,容易出错,找了一下,有VBA可以直接搞定,和大家一起分享。

只要把下面代码里的路径换成你自己csv文件的路径就可以。

 

Option Explicit


Sub ImportCSVs()

‘Author:    Jerry Beaucaire

‘Date:      8/16/2010

‘Summary:   Import all CSV files from a folder into separate sheets

‘           named for the CSV filenames

Dim fPath   As String

Dim fCSV    As String

Dim wbCSV   As Workbook


fPath = "C:\2010\Import\"           ‘path to CSV files, include the final \

Application.ScreenUpdating = False  ‘speed up macro


fCSV = Dir(fPath & "*.csv")         ‘start the CSV file listing


    Do While Len(fCSV) > 0

        Set wbCSV = Workbooks.Open(fPath & fCSV)  ‘open a CSV file and move

        ActiveSheet.Move After:=ThisWorkbook.Sheets(Sheets.Count)

       

        fCSV = Dir                  ‘ready next CSV

    Loop

  

Application.ScreenUpdating = True

Set wbCSV = Nothing

End Sub

参考:

https://sites.google.com/a/madrocketscientist.com/jerrybeaucaires-excelassistant/merge-functions/csvs-to-sheets

Leave a Reply

Your email address will not be published. Required fields are marked *