How to set specific cells to not calculate in Excel?
We have a sheet with many complex formulas (over 6000 vlookups). A macro is run on the sheet and it loops through each of the 3000+ rows multiple times per day for 200+ days. On each update, all of the formulas have to recalculate in order for the macro to continue. Aside from hard-coding the values, is there a way to select certain cells (with formulas in them), and turn off the calculations for them, because their values will not change for the remainder of the run?
We're not looking to disable calculations for the whole sheet, just for a subset of ranges within the sheet. The rest of the sheet should calculate normally.
I don't think you can set specific cells to not calculate. You can, however, temporarily replace the formulas with the actual values within your macro. This would, in practice, have the same effect. There are many ways to accomplish this, here's one idea.
Make a sheet containing all the formulas of the range you do not wish to calculate, but without the =
, so they don't calculate. Then in your macro you can do this to get rid of your formulas:
With Worksheets("sheetname").Range("A1:A100")
.Formula = .Value
End With
This is basically equivalent to copying the range and doing a Paste Special >> Values.
The way you get your formulas back differs depending on whether or not all the formulas are the same. If they are the same use:
With Worksheets("sheetname").Range("A1:A100")
.Formula = "=" & Worksheets("formulasheet").Range("A1:A100").Text
End With
If they are different you can loop through all the cells like this:
Dim i&, firstrow&, lastrow&
Dim ws As Worksheet, formulaws As Worksheet
Set ws = Sheets("sheetname")
Set formulaws = Sheets("formulasheet")
firstrow = 1
lastrow = 100
For i = firstrow To lastrow
ws.Cells(i, 1).Formula = "=" & formulaws.Cells(i, 1).Value
Next i