@Alex,您可以将计算延迟为@Gary 的答案。但是,您之所以问这个问题,是因为您在分配公式时需要 "SPEED to CYCLE through cells",对吗?
如果是,从我的角度来看,如果您 不 使用公式,直到 ALL 公式在 Excel 表中分配,您将获得 通过使用数组一次编写所有公式(VBA 中的一个步骤)来提高速度。
过程是:首先将所有公式放入一个VBA字符串数组,然后再使用例如Range("B1:B100").Formula = ArrayWithFormulas。在该示例中,您一次分配 100 个公式,中间没有重新计算。
如果使用数组将所有单元格逐个写入而不是逐个单元格写入,您将看到速度有很大提高! (如果你有很多单元格要通过,不要循环使用cells(r,c+i))。这里有一个例子:
Sub CreateBunchOfFormulas()
Dim i As Long
Dim ARRAY_OF_FORMULAS() As Variant 'Note: Don't replace Variant by String!
ReDim ARRAY_OF_FORMULAS(1 To 100, 1 To 1)
' For Vertical use: (1 to NumRows,1 to 1)
' for Horizontal: (1 to 1,1 to NumCols)
' for 2D use: (1 to NumRows,1 to NumCols)
'Create the formulas...
For i = 1 To 100
ARRAY_OF_FORMULAS(i, 1) = "=1+3+" & i ' Or any other formula...
Next i
' <-- your extra code here...
' (New formulas won't be calculated. They are not in the Excel sheet yet!
' If you want that no other old formula to recalculate use the old trick:
' Application.Calculation = xlCalculationManual )
'At the very end, write the formulas in the excel at once...
Range("B1:B100").Formula = ARRAY_OF_FORMULAS
End Sub
如果您希望新公式有额外的延迟,那么您可以使用@Gary 技巧,但适用于范围,而不是单个单元格。为此,以' 开头的公式,如'=1+2,并在末尾添加以下代码:
'... previous code, but now formulas starting with (')
Range("B1:B100").Formula = ARRAY_OF_FORMULAS
'Formulas not calculated yet, until next line is executed
Range("B1:B100").Value = Range("B1:B100").Value ' <~~ @Gary's trick
End Sub
最后,一个小片段:如果您的公式是水平排列的(意味着一个公式用于 A 列,另一个用于 B 列等)并且只有少数列,那么您可以记住较短的版本之前的代码:
Dim a as Variant 'Note that no () needed
a = Array("=1+3","=4+8","=5*A1","=sum(A1:C1)")
Range("A1:D1").Formula = ARRAY_OF_FORMULA ' Just a single row
' or...
Range("A1:D100").Formula = ARRAY_OF_FORMULA ' If you want to repeat formulas
' in several rows.
最后,如果您想要一种在公式中使用相对引用的简单方法...
希望这会有所帮助!