Batch convert .xls to .xlsx with VBA without opening the workbooks

后端 未结 1 797
[愿得一人]
[愿得一人] 2021-01-06 10:51

I have a bunch of Excel-Workbooks in the old .xls format. I\'d like to convert them to .xlsx using VBA. The following code accomplishes this task b

相关标签:
1条回答
  • 2021-01-06 11:39

    Here is the piece of code to get what you are looking for:

    Sub ChangeFileFormat()
    
        Dim strCurrentFileExt   As String
        Dim strNewFileExt       As String
        Dim objFSO              As Object
        Dim objFolder           As Object
        Dim objFile             As Object
        Dim xlFile              As Workbook
        Dim strNewName          As String
        Dim strFolderPath       As String
    
        strCurrentFileExt = ".xls"
        strNewFileExt = ".xlsx"
    
        strFolderPath = "C:\Users\Scorpio\Desktop\New folder"
        If Right(strFolderPath, 1) <> "\" Then
            strFolderPath = strFolderPath & "\"
        End If
    
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objFolder = objFSO.getfolder(strFolderPath)
        For Each objFile In objFolder.Files
            strNewName = objFile.Name
            If Right(strNewName, Len(strCurrentFileExt)) = strCurrentFileExt Then
                Set xlFile = Workbooks.Open(objFile.Path, , True)
                strNewName = Replace(strNewName, strCurrentFileExt, strNewFileExt)
                Application.DisplayAlerts = False
                Select Case strNewFileExt
                Case ".xlsx"
                    xlFile.SaveAs strFolderPath & strNewName, XlFileFormat.xlOpenXMLWorkbook
                Case ".xlsm"
                    xlFile.SaveAs strFolderPath & strNewName, XlFileFormat.xlOpenXMLWorkbookMacroEnabled
                End Select
                xlFile.Close
                Application.DisplayAlerts = True
            End If
        Next objFile
    
    ClearMemory:
        strCurrentFileExt = vbNullString
        strNewFileExt = vbNullString
        Set objFSO = Nothing
        Set objFolder = Nothing
        Set objFile = Nothing
        Set xlFile = Nothing
        strNewName = vbNullString
        strFolderPath = vbNullString
    End Sub
    

    this is the link for XL file format : https://msdn.microsoft.com/en-us/library/office/ff198017.aspx

    '-----------------------------------------

    A bit Modification: Check this code, i have only changed its extension name, but please check it with the compatibility... and let me know is it working for you...

    Sub ChangeFileFormat_V1()
    
        Dim strCurrentFileExt   As String
        Dim strNewFileExt       As String
        Dim objFSO              As Object
        Dim objFolder           As Object
        Dim objFile             As File  'Object
        Dim xlFile              As Workbook
        Dim strNewName          As String
        Dim strFolderPath       As String
    
        strCurrentFileExt = ".xls"
        strNewFileExt = ".xlsx"
    
        strFolderPath = "C:\Users\Scorpio\Desktop\New folder"
        If Right(strFolderPath, 1) <> "\" Then
            strFolderPath = strFolderPath & "\"
        End If
    
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objFolder = objFSO.getfolder(strFolderPath)
        For Each objFile In objFolder.Files
            strNewName = objFile.Name
            If Right(strNewName, Len(strCurrentFileExt)) = strCurrentFileExt Then
                strNewName = Replace(strNewName, strCurrentFileExt, strNewFileExt)
                Application.DisplayAlerts = False
                objFile.Name = strNewName
                Application.DisplayAlerts = True
            End If
        Next objFile
    
    ClearMemory:
        strCurrentFileExt = vbNullString
        strNewFileExt = vbNullString
        Set objFSO = Nothing
        Set objFolder = Nothing
        Set objFile = Nothing
        Set xlFile = Nothing
        strNewName = vbNullString
        strFolderPath = vbNullString
    End Sub
    
    0 讨论(0)
提交回复
热议问题