2

I have a macro for Excel, nothing too fancy, no objects, just plain old cycles with formulas. Basically it's this:

I cycle through rows in one column, assigning pretty big SUMPRODUCT functions to the cells without executing them. Once I cycled through the row I do Range.Value = Range.Value twice to execute the formulas and to save result as value. Then I go for the next column.

With each column memory used by Excel increases significantly and after the macro is done and I save the file - the memory is still used (if I can trust Task Manager). After I close and reopen the file - all memory is, of course, freed and I can continue to work. I've reached a point where I can't process all the columns I need on a 32 bit Excel due to memory limit.

I was wondering, maybe there's some trick I could implement between the columns to make Excel forget whatever it keeps remembering? I don't really care if it will hit performance (in case it has cached anything useful for further calculations), I just need to keep memory usage from growing too huge to process. I googled a bit but all the advises are related to setting big objects to nothing - I don't really set much of anything to anything, just cycle through, so that probably won't apply to my case.

Code is like this (don't have it on me right now, but it's a general simplified version of what is in it):

for i = 1 to 12
    ThisWorkbook.ActiveWorksheet.Range(Cells(1,i),Cells(x,i)).Font.Color = vbWhite

    for m = 1 to x
        ThisWorkbook.ActiveWorksheet.Cells(m, i).Formula = "'=SUMPRODUCT(blah)"
    next m

    ThisWorkbook.ActiveWorksheet.Range(Cells(1,i),Cells(x,i)).Value = ThisWorkbook.ActiveWorksheet.Range(Cells(1,i),Cells(x,i)).Value
    ThisWorkbook.ActiveWorksheet.Range(Cells(1,i),Cells(x,i)).Value = ThisWorkbook.ActiveWorksheet.Range(Cells(1,i),Cells(x,i)).Value

    ThisWorkbook.ActiveWorksheet.Range(Cells(1,i),Cells(x,i)).Font.Color = vbBlack
next i

Basically, I color them white so I don't see the messy function text, add function, execute it, save as values, color text black. Important addition: I use SUMPRODUCT to sum some cells from closed files.

4

2 回答 2

6

This isn't really enough code to help you improve the efficiency. I can point out a few tips for you now though.

The first thing to remember is to switch application screen updating to false at the start of the macro.

Application.ScreenUpdating = False

then switch it back just before the end of the macro code

Application.ScreenUpdating = True

Changing the font color is unnecessary now. Your screen will be locked for refreshing while the macro is being executed so you will not be seeing the changes until the macro has finished working.

Consider, disabling Events too.

Application.EnableEvents = False and Application.EnableEvents = true same idea as with the screen updating.

The second thing is to make use of Evaluate() function. Please google it and read about how efficient that function can be.

Instead of putting a function into cell and then doing .Value = .Value you can just Evaluate() an expression and return the result straight to the cell which is much faster!

So you could do something like

for i = 1 to 12
    ThisWorkbook.ActiveWorksheet.Range(Cells(1,i),Cells(x,i)).Value = Evaluate("=SUM(blah)")
next i

I hope you get the idea.

Here are a few links with tips on how to speed up your macro:

于 2013-10-22T10:11:46.490 回答
2

I know this is an old question but a way that seems to work is saving the Workbook. Run ThisWorkbook.Save every once in a while and if you monitor EXCEL in the Task Manager you will notice that it clears up a significant amount of memory.

It seems as though Excel may be caching certain function calls or something and saving clears this cache.

于 2016-04-27T12:19:46.783 回答