简体   繁体   中英

does size of excel workbook matters running a vba code?

Does the size of excel workbook effect the running of a VBA code? I have written a VBA code into a excel workbook whose size is 200 MB.The excel workbook has the browse button and these browse button picks another Excel file and performs operations and write backs some of the needed contents to the 200 MB size file. But these operation is too slow taking 8 to 13 min. These excel workbook contains charts, graphs and more modules and 16 sheets and pivot tables lots of stuff.(will these effect to run the vba code?)

But the same code I have written to another new excel workbook which is of size 200KB with the same browse button and all that stuff and here it took 4 seconds to finish. I wonder whats going wrong. Using for loops is harmful for a big file but I cannot perform my operation using without a loop in my code.If for loop is harmful then it should even effect 200KB size but no danger with that! what I have to do now?

Make sure you have used Application.ScreenUpdating=False and Application.Calculation=xlManual before the code that writes to the workbook. (Then reset them at the end).

Also there is a big overhead on each read and write to a Range, so its usually much faster to read a large block of data into a variant.

Dim vArr as Variant
vArr=Worksheets("Sheet1").Range("A1:Z9999").Value2

'Manipulate the resulting array.
For j = LBound(vArr) To UBound(vArr)
   For k = LBound(vArr,2) To UBound(vArr,2)
      vArr(j,k)=vArr(j,k)*2
   Next k
Next j

'And then write it back to its destination.
Worksheets("Sheet2").Range("A1:Z9999")=vArr

在如此大的文件中,即使仅仅是读取和/或写入文件的动作也要花费大量的时间,许多中间操作也可能会受到影响。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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