.xls2.xlsx 如何把一个文件夹下面的.xls文件快速转换成.xlsx

工具:EXCEL/VBA

问题:昨天用desktop导入几个报价文件,其中有些是老格式的.xls,有些是新格式的,在用Power BI desktop整体导入出现了问题,如下图,

.xls2.xlsx如何把一个文件夹下面的.xls文件快速转换成.xlsx

.xls格式导入后出错

就想着看看能不能上网找个VBA程序,把这个文件夹下面的老格式文件自动更改成新格式,再整体导入,然后可以组合并加载。。。。

结果今天上午搜了半小时,居然没有找到,无奈自己动手写了一个,好久没有动手写程序了,又花费一小时。方法比较乱,仅供参考。

程序如下:

Sub XlsToXlsx()

Dim Wb As Workbook

Dim myfile As Variant

Application.ScreenUpdating = False

'close screen auto updating

Application.DisplayAlerts = False

'close the alerts auto display

Dim sht As Worksheet, Path1 As String

With Application.FileDialog(msoFileDialogFolderPicker)

'choose the file foder which the .xls files are in

If .Show Then Path1 = .SelectedItems(1) Else Exit Sub

'read the .xls file, or exit the program if you didnt choose a folder

End With

If Right(Path1, 1) <> "\" Then Path1 = Path1 & "\"

'check the last letter is "\" or not, if not, make it "\"

myfile = Dir(Path1 & "*.xls")

'get 1st .xls file name

Debug.Print myfile

'debug, and print the 1st file name out

Do While Application.WorksheetFunction.And(Len(myfile) <> 0, myfile <> ThisWorkbook.Name)

myfilepath = Path1 + myfile

newMyfilepath = myfilepath & "x"

'new file name to ".xlsx" plus old path

Excel.Application.Workbooks.Open Filename:=myfilepath

'open .xls file

Windows(myfile).Activate

ActiveWorkbook.SaveAs newMyfilepath, xlOpenXMLWorkbook

'save as .xlsx name file

ActiveWorkbook.Close

myfile = Dir

'get next .xls file name

Loop

Application.ScreenUpdating = True

Application.DisplayAlerts = True

'trun on the screen auto updating, and alerts auto display

End Sub