FormulaR1C1 #NAME错误

我已经开始为我的考试学习VBA,每当我运行这个代码,而不是在行的末尾的SUM我得到#NAME错误。 如果我点击它并刷新,它将按照预期进行计算。

有什么我做错了吗?

(由于语言设置,SZUM而不是SUM)

Sub RandomDice() Const min = 2 Const max = 10 Dim c As Integer, r As Integer 'Random between 2 and 10 for the column and row numbers c = Int(Rnd() * (max - min + 1)) + 1 r = Int(Rnd() * (max - min + 1)) + 1 'Clear the content with max+1 size range Range(Cells(1, 1), Cells(max + 1, max + 1)).Clear 'Filling the cells with numbers between 1 and 6 Dim i As Integer, j As Integer For i = 1 To r For j = 1 To c Cells(i, j) = Int(6 * Rnd() + 1) Next j Next i 'Calculate the SUM for each row Cells(1, c + 1).FormulaR1C1 = "=SZUM(RC[-" + Trim(Str(c)) + "]:RC[-1])" Cells(1, c + 1).Copy Range(Cells(1, c + 1), Cells(r, c + 1)).PasteSpecial End Sub 

错误的图片

VBA非常以美国为中心,无论您的系统的区域设置如何使用(例如)或您的Office安装所使用的语言,都需要英文函数名称和逗号分隔列表分隔符。

要使用公式,因为它们将出现在工作表上,请使用Range.FormulaLocal属性或Range.FormulaR1C1Local属性 。

 'in HU-HU¹ Cells(1, c + 1).FormulaR1C1Local = "=SZUM(RC[-" + Trim(Str(c)) + "]:RC[-1])" 'or in EN-US, Cells(1, c + 1).FormulaR1C1 = "=SUM(RC[-" + Trim(Str(c)) + "]:RC[-1])" 

对于xlA1语法,等效属性是Range.Formula和Range.FormulaLocal属性 。


¹SZUMfüggvény