VBA代码优化

我有VBA的代码和大量的行它使用太多的内存 – 它可能需要几GB,并暗恋。 代码数量请求服务器为XLMfind一些数据,并写入比去另一个数字。 如果有超过500行它粉碎。 你能帮我优化代码来处理大约10000行吗? 感谢您的帮助Marek

Sub ares() Application.Calculation = xlCalculationManual Application.ScreenUpdating = False 'potlačí obnovování obrazovky Application.DisplayAlerts = False 'potlačí varovné hlášky Application.DisplayStatusBar = False Application.EnableEvents = False ActiveSheet.DisplayPageBreaks = False Dim i As Integer Dim row As Integer Dim column As Integer For i = 2 To 15000 Sheets.Add(After:=Sheets(Sheets.Count)).Name = "ares" Sheets("ares").Activate On Error GoTo ErrorHandler ActiveWorkbook.XmlImport URL:="http://wwwinfo.mfcr.cz/cgi-bin/ares/darv_bas.cgi?ico=" & Worksheets("ico").Cells(i, 1).Value, ImportMap:=Nothing, Overwrite:=True, Destination:=Range("$A$1") If Worksheets("ares").Cells(2, 10).Value = "" Then Worksheets("ico").Cells(i, 2).Value = "OK" row = 2 column = 3 Do While Worksheets("ares").Cells(row, 1).Value <> "" If Worksheets("ares").Cells(row, 167).Value <> "" Then Worksheets("ico").Cells(i, column).Value = Worksheets("ares").Cells(row, 167).Value column = column + 1 End If row = row + 1 Loop Else Worksheets("ico").Cells(i, 2).Value = Worksheets("ares").Cells(2, 10).Value End If ErrorResume: Sheets("ares").Delete Next i Application.ScreenUpdating = True 'zapne obnovování obrazovky Application.DisplayAlerts = FaTruelse 'obnoví varovné hlášky Application.Calculation = xlCalculationAutomatic Application.DisplayStatusBar = True Application.EnableEvents = True ActiveSheet.DisplayPageBreaks = True Exit Sub ErrorHandler: Worksheets("ico").Cells(i, 2).Value = "Jiná chyba" Resume ErrorResume End Sub 

删除Sheets("ares").Activate并将Integer更改为Long Integer 。 应该够了。

只要您在任何地方正确地引用了工作表,就不需要激活工作表。

关于Integer,它给你一个小的加速 – 为什么使用Integer而不是Long?