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

相关推荐

  1. VBA 批量转换xls文件

    2024-07-10 00:22:06       21 阅读
  2. VBA 批量处理Excel文件

    2024-07-10 00:22:06       34 阅读
  3. EXCEL VBA 多sheet批量转转PDF

    2024-07-10 00:22:06       29 阅读
  4. 使用VBA将多个txt批量转换成excel表并保存

    2024-07-10 00:22:06       42 阅读
  5. 如何使用 FFmpeg 批量转换文件夹内的所有文件

    2024-07-10 00:22:06       43 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 00:22:06       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 00:22:06       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 00:22:06       58 阅读
  4. Python语言-面向对象

    2024-07-10 00:22:06       69 阅读

热门阅读

  1. 逻辑回归不是回归吗?那为什么叫回归?

    2024-07-10 00:22:06       20 阅读
  2. 架构设计(1)分布式架构

    2024-07-10 00:22:06       16 阅读
  3. 总账清账(不包含客户/供应商清账)

    2024-07-10 00:22:06       18 阅读
  4. Vue3--Watch、Watcheffect、Computed的使用和区别

    2024-07-10 00:22:06       20 阅读
  5. react apollo hooks

    2024-07-10 00:22:06       21 阅读