VBA 批量转换xls文件

将.xls格式的Excel文件批量转换成.xlsx或.xlsm(没有宏时转换成.xlsx,有宏时转换成.xlsm)

Option Explicit

Sub BatchConvertXLSFiles()
    Dim wb As Workbook
    Dim strSelectedPath As String
    Dim strFileFullPath As String
    Dim strFile As String
    
On Error GoTo ErrProcess

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    With Application.FileDialog(msoFileDialogFolderPicker)
        .InitialFileName = ThisWorkbook.Path & "\"
        .Title = "Select Path"
        If .Show Then
            strSelectedPath = .SelectedItems(1)
            strFileFullPath = Dir(strSelectedPath & "\*.xls")
            Do While strFileFullPath <> ""
                If Right(strFileFullPath, 3) = "xls" Then
                    strFile = Left(strFileFullPath, InStrRev(strFileFullPath, ".") - 1)
                    Set wb = Application.Workbooks.Open(strSelectedPath & "\" & strFileFullPath, 0)
                    If wb.HasVBProject Then
                        wb.SaveAs Filename:=strSelectedPath & "\" & strFile, FileFormat:=xlOpenXMLWorkbookMacroEnabled
                    Else
                        wb.SaveAs Filename:=strSelectedPath & "\" & strFile, FileFormat:=xlOpenXMLWorkbook
                    End If
                    wb.Close SaveChanges:=False
                End If
                strFileFullPath = Dir()
            Loop
            Set wb = Nothing
        Else
            MsgBox prompt:="Path selection is canceled.", Title:="Select Path"
            Exit Sub
        End If
    End With
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    
    MsgBox "Done."
    Exit Sub
ErrProcess:
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    MsgBox Err.Description
End Sub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值