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

问题

有没有比循环文件夹/打开/保存/关闭更快的方法来转换文件格式?

4

1 回答 1

3

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

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

在适当的部分。如果您有任何其他由各种 Excel 事件(如打开或关闭工作簿等)触发的加载项或宏,这将特别有用。

于 2012-10-09T11:05:25.550 回答