Office-365

对于用Office 365的用户看到电脑上很多的xls文件,总是觉得不爽,应为xls占用空间总是比xlsx大一些,文件越大体积减少越明显。用另存为的方法转换起来又很慢,于是想找一个软件批量转换。电脑上的office tool只支持32位版本office。
于是网上搜素一番,发现可以用VBA代码。步骤如下:

1、打开Excel程序

打开一个新的excel窗口

2、打开Microsoft Visual Basic

按住 ALT + F11 键打开 Microsoft Visual Basic应用程序 窗口。

3、插入模块

点击 插页 > 模块,然后将以下代码粘贴到 模块 窗口。

VBA代码:一次将多种xls格式转换为xlsx格式

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 = "请选择要转换的xls文件所在的文件夹:"
.InitialFileName = "C:\"
End With
If xSFD.Show <> -1 Then Exit Sub
xSPath = xSFD.SelectedItems.Item(1)
Set xRFD = Application.FileDialog(msoFileDialogFolderPicker)
With xRFD
.Title = "请选择转换成xlsx后要保存的文件夹:"
.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文件的文件夹。