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