转换速度缓慢 批量将 .xlsx 转为 .xls

3
我已经编写了这段代码,用于批量将一堆 .xlsx 工作簿转换为 .xls(我有很多终端用户使用 2003 版本)。虽然它能完成任务,但速度非常慢。我在本地测试了20个工作簿,每个文件大小只有约30 kb,执行时间为9.78秒。通过我的 SharePoint 服务器,这需要262秒,但我认为 SharePoint 的极慢速度是另一个问题。 代码:
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

问题

是否有一种比循环遍历文件夹/打开/保存/关闭更快的方法来转换文件格式?


Microsoft Office 兼容性包可以帮助您避免转换文件的麻烦。请访问 https://www.microsoft.com/en-us/download/details.aspx?id=3 以获取下载链接。 - SeanC
真的,但这样可以避免所有最终用户都必须安装它,对于一些人来说可能超出了他们的能力范围。 - Alistair Weir
你的xlsx文件是否有格式或只是纯数据? - Siddharth Rout
我目前正在应用它的文件只包含纯数据。 - Alistair Weir
2
为什么不使用OLEDB将xlsx文件转换为CSV呢?转换会在眨眼间完成,文件可以在Excel 2003中读取。 - Siddharth Rout
显示剩余3条评论
1个回答

3
如果这仍然相关。当我遇到类似问题时,最终得到的代码几乎相同,但是帮助我加速一点的是禁用Application.EnableEvents,即:

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

在适当的部分中使用它。如果您有任何其他的插件或宏,这将特别有帮助,因为它们会被各种Excel事件触发,如打开或关闭工作簿等。


是的,这确实可以稍微加快速度,但我试图找到一种不同的方法,我认为这并不可能。 - Alistair Weir

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接