2017-03-29 1 views
1

나는 수백 개의 매크로가있는 복잡한 Excel 통합 문서를 가지고 있습니다. 모두 잘 돌아갑니다. 크기는 9MB입니다. 추가 시트 (복잡한 차트)를 추가하면 파일 크기가 14MB로 증가합니다. 이것은 문제가되지 않지만 일부 코드는 매우 느리게 실행됩니다. 특히 이것은 한 시트에서 다른 시트로 약 60 개의 셀을 복사하는 복사 시퀀스입니다. 일반적으로 약 5 초 후에 발생하지만 파일 크기가 14MB가되면 코드에 약 60 초가 걸립니다. 시트를 삭제하면 문제가 해결됩니다. 복사 순서는 내가 삭제 한 시트와 아무런 관련이 없습니다. 내가 뭘 놓치고 있니?파일 크기가 vba 실행 속도에 영향을 줍니까?

+2

설정을 전환 고려 = 사실 'Application.ScreenUpdating 사용 = FALSE','application.calculation = xlManual', 그리고'application.enableevents을 '. VBA가 끝나면 다시 false로 설정하십시오. 그것들은 화면의 업데이트를 막고, 응용 프로그램이 수식을 업데이트하지 못하도록하며, 코드가 실행되는 동안 워크 시트 나 워크 북 vba 이벤트가 실행되지 않도록합니다. – JNevill

+0

감사합니다. 하지만 모든 설정이 당신이 설명하는 것처럼 공통 분모가 파일 크기 인 것처럼 보이는 것은 아무 것도 아닙니다. –

답변

0

매크로

« Turn off calculation and screen refresh while macro is running. Be sure to restore settings when the macro ends. Some supposedly reset automatically but later releases tend to require you to reset. Macros may terminate abnormally so don’t use without that thought in mind. The examples below do not save current settings and restore them but assume normal usage is in effect. 

Screen flickering is usually the result of bad coding, but turning off Screen Updating will eliminate the blinking, and speed up a macro. Turning off calculation can have a more dramatic effect on improving performance. Use of Special Cells and restriction to used range will greatly improve coding. 

Turning off ScreenUpdating and Calculation are the easiest changes to implement. 

    Application.ScreenUpdating = False 
    Application.Calculation = xlCalculationManual 
     ' ooo Your code here ooo 
    Application.Calculation = xlCalculationAutomatic 
    Application.ScreenUpdating = True 


    Prior to XL97 (Excel 5 and Excel 97) use 
    Application.Calculation = xlManual   'prior to XL97 
    Application.Calculation = xlAutomatic  'prior to XL97 
       alternative coding: restores previous settings: 

     Dim savCalc As Long, savScrnUD As Boolean 
     savCalc = Application.Calculation 
     savScrnUD = Application.ScreenUpdating 
     Application.Calculation = xlCalculationAutomatic 
     Application.ScreenUpdating = True 
     ' ooo Your code here ooo 
    done: 
     Application.Calculation = savCalc 
     Application.ScreenUpdating = savScrnUD 

I always want calculation on and there are too many bad macros that fail and macro of my own that fail during testing so I use the version on the left above. The code on the right above is more correct but prone to problems of leaving calculation off. 

For most VBA actions, you don’t have to select the sheet or cell. Look for such coding to be reworked/removed, you might even be able to remove the need to suppress screen updating if the appearance of the screen is not changed. (Brian Wilson Example: 2000-12-28) 

Are there any Worksheet_Change events. 

Related to last row problems, macros can take forever to run if they loop through all possible columns and rows instead of restricting activity to the used area. Examples where such failures commonly occur: deleting rows with certain content, inserting rows or columns, selecting an entire column and macro processes entire column instead of cells in the active area. 

In a test of one macro adding DIM statements appeared to increase time 30% contrary to good coding practices. Nevertheless, by dimensioning variables you simplify code writing and maintenance and avoid some additional problems. Turning off screenupdating saved about 12%, but turning off calculation saved an additional whopping 75%. 

Use SpecialCells to reduce selection to cell types of interest (constants, formulas, comments, visible, etc.). SpecialCells is automatically limited to the used range, which eliminates processing every single cell in a selected column, if a column is selected. Examples range from 366 seconds without using SpecialCells, to .040 seconds by reducing selection with SpecialCells as seen on Proper, and other Text changes -- Use of Special Cells also see notations on same page. 

Dimensioning variables for use with Options Explicit. (#dimensioning) If you have simply used Dim xyz as variant you can find out the actual type that you used with MsgBox typename(xyz) so you can replace variant by its actual type. (see vba.htm for more information). 

은 VBA 코드를 (#speedupvba)을 가속화 슬로우 (#slowmacros)를 실행

The biggest things were in the previous topic turning off calculation and display. Here are some additional tips. 

Declare variables with their datatype [see table]: Byte, Boolean, Long, Long, Currency, Decimal, Single, Double, Date, String, Object, Variant (default), and user-defined types, as well as specific types of objects. A common pitfall is failing to include attributes for each variable on a DIM statement resulting in a data type of variant. 

Use long in preference to integer and use double in preference to single as these are what the system actually has to use, and use String instead of Byte. Also note that anything referring to rows should be immediately changed to long, since 65,536 exceeds the limit of the integer datatype. 

Avoid changing the activecell or the active page, unless that is the desired result. Example to create a new entry after last entry in Column A, not to be confused with last cell row (Ctrl+End), which can in itself be problematic. (#samplesub) 

Sub SampleSub() 
    Cells(Rows.Count,Range("A1").Column).End(xlUp).Offset(1,0).Value _ 
    ="** New Entry**" 
End Sub 

모든 .XLSB의 도움으로 파일을 저장합니까?

마지막으로 다음 링크를 고려하십시오.

http://professor-excel.com/15-ways-to-speed-up-excel/

http://dmcritchie.mvps.org/excel/slowresp.htm

http://chandoo.org/wp/2014/01/17/big-trouble-in-little-spreadsheet/

https://msdn.microsoft.com/en-us/library/office/ff726673(v=office.14).aspx

관련 문제