vba: csv文件批量转换为xlsx的宏


1、新建一个excel


2、选择alt+F11


3、选择insert model


4、输入程序,选择包含csv文件的文件夹



1 Sub CSVtoXLS() 2 'UpdatebyExtendoffice20170814 3 Dim xFd As FileDialog 4 Dim xSPath As String 5 Dim xCSVFile As String 6 Dim xWsheet As String 7 Application.DisplayAlerts = False 8 Application.StatusBar = True 9 xWsheet = ActiveWorkbook.Name 10 Set xFd = Application.FileDialog(msoFileDialogFolderPicker) 11 xFd.Title = "Select a folder:" 12 If xFd.Show = -1 Then 13 xSPath = xFd.SelectedItems(1) 14 Else 15 Exit Sub 16 End If 17 If Right(xSPath, 1) <> "\" Then xSPath = xSPath + "\" 18 xCSVFile = Dir(xSPath & "*.csv") 19 Do While xCSVFile <> "" 20 Application.StatusBar = "Converting: " & xCSVFile 21 Workbooks.Open Filename:=xSPath & xCSVFile 22 ActiveWorkbook.SaveAs Replace(xSPath & xCSVFile, ".csv", ".xlsx", vbTextCompare), xlNormal 23 ActiveWorkbook.Close 24 Windows(xWsheet).Activate 25 xCSVFile = Dir 26 Loop 27 Application.StatusBar = False 28 Application.DisplayAlerts = True 29 End Sub

 

上一篇:《SeleniumBasic 3.141.0.0 - 在VBA中操作浏览器》高级技术之四:SeleniumBasic的三种等待方式


下一篇:VBA实例:高考分数投档指数分析(模块2.过程部分)