Home » excel » excel – Slow Execution Time Batch .xlsx to .xls Conversion

excel – Slow Execution Time Batch .xlsx to .xls Conversion

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have written this code to batch convert a bunch of .xlsx workbooks to .xls (I have a lot of end users using 2003). It does the job but it is very slow, I tested this on 20 workbooks each only about 30 kb in size and it took 9.78 seconds to execute locally. Through my sharepoint server this took 262 seconds but I believe the incredibly slow speed for sharepoint is a different issue.

Code

Option Explicit
Sub Convert_to972003()
    Dim orgwb As Workbook
    Dim mypath As String, strfilename As String
    Dim nname As String

    '--> Error Handling
    On Error GoTo WhatHappened

    '--> Disable Alerts
    With Application
        .DisplayAlerts = False
        .ScreenUpdating = False
    End With

    '--> Specify location of workbooks
    mypath = "C:\xxx"
    strfilename = Dir(mypath & "\*.xlsx", vbNormal)

    '--> Check the specified folder contains files
    If Len(strfilename) = 0 Then Exit Sub

    '--> Start Loop, end when last file reached
    Do Until strfilename = ""

    '--> Open a workbook
        Set orgwb = Application.Workbooks.Open _
        (mypath & "\" & strfilename)

        '--> Create new Filename, Save in new File Format and Close
        nname = Replace(strfilename, ".xlsx", ".xls")
        orgwb.SaveAs mypath & "\" & nname, FileFormat:=xlExcel8
        orgwb.Close
        strfilename = Dir()
    Loop

    '--> Enable Alerts
    With Application
        .DisplayAlerts = True
        .ScreenUpdating = True
    End With

Exit Sub

WhatHappened: MsgBox Err.Description

End Sub

Question

Is there a quicker method to convert file formats than looping through the folder/opening/saving/closing?

How to&Answers:

If this is still relevant. When I had a similar problem I ended up with nearly the same code, but what helped me to speed it up a little was to disable Application.EnableEvents, i.e.:

Application.EnableEvents = False
...
Application.EnableEvents = True

in the appropriate parts. This is especially helpful if you have any other add-ins or macros which are triggered by various Excel events like opening or closing a workbook etc.