简体   繁体   English

为什么我的Html到Excel转换在循环浏览文件时会随着时间的推移而减慢使用VBA?

[英]Why my Html to Excel conversion slows down using VBA over time when looping through files?

I need to convert html files to excel from time to time. 我需要不时将html文件转换为excel。 There are approximately 9000 html files that has tables on it. 大约有9000个html文件,上面有表格。

I find it useful to convert them using excel 2007 vba, and made a macro to do the job, I have taken into account a bug of excel that halts the macro on Workbooks.Open function when SHIFT key is pressed, aside from that I disabled alerts, events and screen updating and make invisible the application as I don't want to interrupt me while I do something else. 我发现使用excel 2007 vba转换它们很有用,并且做了一个宏来完成这项工作,我已经考虑了一个excel的错误,当按下SHIFT键时暂停Workbooks.Open函数上的宏,除了我禁用了警报,事件和屏幕更新,使应用程序不可见,因为我不想在我做其他事情时打扰我。

'Declare API
Declare Function GetKeyState Lib "User32" _
(ByVal vKey As Integer) As Integer
Const SHIFT_KEY = 16

Function ShiftPressed() As Boolean
'Returns True if shift key is pressed
    ShiftPressed = GetKeyState(SHIFT_KEY) < 0
End Function

Sub ConvertHtmlToExcel()
    Dim wb As Workbook
    Dim strFile As String
    Dim strPath As String

    With Application
        .EnableEvents = False
        .DisplayAlerts = False
        .ScreenUpdating = False
        .Visible = False
    End With

    strPath = "c:\FolderToConvert\"
    strFile = Dir(strPath & "*.html")

    Do While strFile <> ""
        Do While ShiftPressed()
            DoEvents
        Loop
        Set wb = Workbooks.Open(strPath & strFile)
        strFile = Mid(strFile, 1, Len(strFile) - 5) & ".xls"
        wb.SaveAs strPath & strFile, XlFileFormat.xlWorkbookNormal
        wb.Close
        Set wb = Nothing
        strFile = Dir
    Loop

    With Application
        .EnableEvents = True
        .DisplayAlerts = True
        .ScreenUpdating = True
        .Visible = True
    End With
End Sub

The macro seems to be fine but when running I have the following conversion rate per minute: 宏看起来很好,但在运行时我每分钟有以下转换率:

  1. 40 40
  2. 31 31
  3. 25 25
  4. 21 21
  5. 19 19
  6. 18 18

And it keeps decreasing right now after 500 file converted its current rate is 8 per minute. 并且它在500转换后立即减少其当前速率为每分钟8。

After 2359 files the rate decreased to 2 per minute, when testing I had visible = true and saw that it took more time to OPEN the workbook. 在2359个文件之后,速率降低到每分钟2次,当测试我有可见= true并且看到它花了更多时间来打开工作簿。

So the problem seem to be on Workbooks.Open that at the beginning of the loop works as fast as it can but in further loops it starts to slow down. 因此问题似乎出现在Workbook上。打开循环开始时尽可能快地工作但是在进一步的循环中它开始变慢。

Has anyone stumbled on this? 谁有人偶然发现了这个? Are there any workaround? 有没有解决方法? Is my code missing something? 我的代码遗漏了什么吗? And sometimes the macro still halts the execution I assume that the shift key wasn't catched by that function. 有时宏仍然会停止执行,我认为Shift键没有被该函数捕获。

There is no standard way to override the "shift bypass" function within Excel (in contrast to the "AllowByPassKey" property in Access). 没有标准方法可以覆盖Excel中的“shift bypass”功能(与Access中的“AllowByPassKey”属性相反)。 If you have problems with the code you applied (I haven't tried it), have you considered creating an add in? 如果您使用的代码有问题(我还没有尝试过),您是否考虑过创建添加?

http://msdn.microsoft.com/nl-nl/library/aa671739(v=vs.71).aspx On the Microsoft site is explicitly mentioned: http://msdn.microsoft.com/nl-nl/library/aa671739(v=vs.71).aspx在Microsoft网站上明确提到:

Several characteristics distinguish an Excel add-in from a typical workbook file: Excel加载项与典型工作簿文件的区别在于:

  • Users cannot use the SHIFT key to bypass events that are built into the add-in. 用户无法使用SHIFT键来绕过加载项中内置的事件。 This feature makes sure any event procedures you have written in the add-in will run at the proper time. 此功能可确保您在加载项中编写的任何事件过程都将在适当的时间运行。

Regarding the speed: you seem to correctly allocate (.open) and release (set to nothing) memory for the "workbook" object in the loop so the memory it takes in the RAM should not increase or slow down during the processes. 关于速度:你似乎正确地为循环中的“工作簿”对象分配(.open)和释放(设置为空)内存,这样它在RAM中占用的内存不应该在进程中增加或减慢。

This interesting thread could provide an explanation: 这个有趣的线程可以提供一个解释:

http://www.add-ins.com/support/out-of-memory-or-not-enough-resource-problem-with-microsoft-excel.htm http://www.add-ins.com/support/out-of-memory-or-not-enough-resource-problem-with-microsoft-excel.htm

"The conclusion we reached from the above testing is that VBA add-ins do not use a significant amount of memory. Workbooks are the major user of memory. And Excel does not release all memory when workbooks are closed. Which supports one approach of closing and restarting Excel after doing a lot of work. You should close and re open Excel at least once every two hours." “我们从上述测试中得出的结论是,VBA加载项不会占用大量内存。工作簿是内存的主要用户。当工作簿关闭时,Excel不会释放所有内存。这支持一种关闭方法并且在完成大量工作后重新启动Excel。您应该每两小时至少关闭并重新打开一次Excel。“

Perhaps it is worth the try to each time set a new application and using application.quit, which clears the entire Excel application used from memory. 也许值得尝试每次设置一个新的应用程序并使用application.quit,它清除从内存中使用的整个Excel应用程序。

 Dim appExcel As Excel.Application

 Set appExcel = CreateObject("Excel.Application")
 appExcel.Workbooks.Add
 'Do smtg
 appExcel.Quit

Hope this works out for you! 希望这对你有用! Interesting question! 有趣的问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM