51

How do you get spreadsheet data in Excel to recalculate itself from within VBA, without the kluge of just changing a cell value?

braX
  • 11,506
  • 5
  • 20
  • 33
Lance Roberts
  • 22,383
  • 32
  • 112
  • 130

6 Answers6

63

The following lines will do the trick:

ActiveSheet.EnableCalculation = False  
ActiveSheet.EnableCalculation = True  

Edit: The .Calculate() method will not work for all functions. I tested it on a sheet with add-in array functions. The production sheet I'm using is complex enough that I don't want to test the .CalculateFull() method, but it may work.

MarredCheese
  • 17,541
  • 8
  • 92
  • 91
Lance Roberts
  • 22,383
  • 32
  • 112
  • 130
  • 1
    Actually, there are .Calculate() and .CalculateFull() methods for that, on different levels. – GSerg Sep 30 '08 at 19:15
  • 2
    I've tested Calculate on a sheet, and it not all functions in a cell recalculate. You can also do it by switching the 'dirty' bit, but I found this to be the easiest solution. – Lance Roberts Sep 30 '08 at 19:26
  • Using the `EnableCalculation` property is a nice idea, because it allows a single sheet to be recalculated on the Worksheet level. In contrast, `Application.CalculateFull` forces a full calculation of all data, not only in a single Worksheet or Workbook, but in **all open workbooks**! This could be major overkill, especially since the user could have several unrelated, potentially complicated, workbooks open. – drwatsoncode Jun 10 '15 at 18:47
  • I wrote an answer about refreshing the **active workbook** without doing so with the other open ones. – Calculus Knight Nov 28 '16 at 11:12
20

This should do the trick...

'recalculate all open workbooks
Application.Calculate

'recalculate a specific worksheet
Worksheets(1).Calculate

' recalculate a specific range
Worksheets(1).Columns(1).Calculate
Graham
  • 14,885
  • 4
  • 36
  • 42
8

Sometimes Excel will hiccup and needs a kick-start to reapply an equation. This happens in some cases when you are using custom formulas.

Make sure that you have the following script

ActiveSheet.EnableCalculation = True

Reapply the equation of choice.

Cells(RowA,ColB).Formula = Cells(RowA,ColB).Formula

This can then be looped as needed.

kambeeks
  • 81
  • 1
  • 2
6

You might also try

Application.CalculateFull

or

Application.CalculateFullRebuild

if you don't mind rebuilding all open workbooks, rather than just the active worksheet. (CalculateFullRebuild rebuilds dependencies as well.)

Dave DuPlantis
  • 6,378
  • 3
  • 26
  • 30
3

I had an issue with turning off a background image (a DRAFT watermark) in VBA. My change wasn't showing up (which was performed with the Sheets(1).PageSetup.CenterHeader = "" method) - so I needed a way to refresh. The ActiveSheet.EnableCalculation approach partly did the trick, but didn't cover unused cells.

In the end I found what I needed with a one liner that made the image vanish when it was no longer set :-

Application.ScreenUpdating = True

AjV Jsy
  • 5,799
  • 4
  • 34
  • 30
1

After a data connection update, some UDF's were not executing. Using a subroutine, I was trying to recalcuate a single column with:

Sheets("mysheet").Columns("D").Calculate

But above statement had no effect. None of above solutions helped, except kambeeks suggestion to replace formulas worked and was fast if manual recalc turned on during update. Below code solved my problem, even if not exactly responsible to OP "kluge" comment, it provided a fast/reliable solution to force recalculation of user-specified cells.

Application.Calculation = xlManual
DoEvents
For Each mycell In Sheets("mysheet").Range("D9:D750").Cells
    mycell.Formula = mycell.Formula
Next
DoEvents
Application.Calculation = xlAutomatic
pghcpa
  • 833
  • 11
  • 25