使用VBA代码批量将xls格式转换为xlsx格式
如果您想一次将多个xls文件转换为xlsx文件,而不是一个个地另存为,请执行以下步骤:
1、打开excel
2、按住 ALT + F11 键打开 Microsoft Visual Basic应用程序 窗口。
3、点击 插入 > 模块,然后将以下代码粘贴到 模块 窗口。
Sub ConvertToXlsx()
'Updateby Extendoffice
Dim strPath As String
Dim strFile As String
Dim xWbk As Workbook
Dim xSFD, xRFD As FileDialog
Dim xSPath As String
Dim xRPath As String
Set xSFD = Application.FileDialog(msoFileDialogFolderPicker)
With xSFD
.Title = "Please select the folder contains the xls files:"
.InitialFileName = "C:\"
End With
If xSFD.Show <> -1 Then Exit Sub
xSPath = xSFD.SelectedItems.Item(1)
Set xRFD = Application.FileDialog(msoFileDialogFolderPicker)
With xRFD
.Title = "Please select a folder for outputting the new files:"
.InitialFileName = "C:\"
End With
If xRFD.Show <> -1 Then Exit Sub
xRPath = xRFD.SelectedItems.Item(1) & "\"
strPath = xSPath & "\"
strFile = Dir(strPath & "*.xls")
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Do While strFile <> ""
If Right(strFile, 3) = "xls" Then
Set xWbk = Workbooks.Open(Filename:=strPath & strFile)
xWbk.SaveAs Filename:=xRPath & strFile & "x", _
FileFormat:=xlOpenXMLWorkbook
xWbk.Close SaveChanges:=False
End If
strFile = Dir
Loop
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
4、 然后按 F5 键运行此代码,将显示一个窗口,请选择一个包含要转换的xls文件的文件夹
5、 然后,点击 OK,会弹出另一个窗口,请选择要输出新转换后文件的文件夹路径,请参见屏幕截图: