2017-10-05 232 views
1

我有VBA中的代碼和大量的行使用太多的內存 - 它可能需要幾GB,並使粉碎。代碼數量請求服務器爲XLM查找一些數據並寫入它們而不是轉到另一個數字。如果超過500行,它會粉碎。你能幫我優化代碼來處理大約10000行嗎? 感謝您的幫助 馬立克VBA代碼優化

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 
+0

'Application.DisplayAlerts = FaTruelse「obnovívarovnéhlášky' - 這是一個新的布爾:) – Vityata

+0

什麼辦法可以導入數據和設置您的ICO細胞而無需添加和刪除表單的時間。也許重用表單? – MacroMarc

回答

0

刪除Sheets("ares").Activate和改變IntegerLong。這應該夠了。

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

關於整數,它給你一個輕微的加速 - Why Use Integer Instead of Long?

+0

我做到了,它可能沒什麼用處,但不夠。如果我使它運行1000行,最終文件大約是60 000 KB,而具有相同數據的正常exel幾乎沒有hunderts KB大,但我無法找出它爲什麼如此之大。 –