excel - 缓慢执行时间批量 .xlsx 到 .xls 转换

标签 excel execution-time vba

我编写了这段代码来将一堆 .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

问题

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

最佳答案

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

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

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

关于excel - 缓慢执行时间批量 .xlsx 到 .xls 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12371077/

相关文章:

ms-access - Access VBA 查找最后一次出现的字符串?

xml - 创建一个可以导入到 EXCEL 的 XSD,具有基于元素值的限制

vba - 如何将工作表(选项卡)名称与单独工作表中的范围匹配并将特定文本返回到每个工作表

excel - VBA 中对特定行和列范围的结构化引用

php - phpMyadmin 中的最大执行时间

sql - 我应该使用什么 sql 数据类型来存储以毫秒为单位的执行时间?

c# - 你能在 ClosedXML (C#) 中填充一个范围吗

java - 如何在 Java 中计时方法的执行?

excel - VBA-运行时错误9并使用*作为变量工作表名称

VBA 宏 workbook.open 或 workbook.activate 通过变量引用