FormulaR1C1 #NAME错误

时间:2016-01-22 21:16:32

标签: excel vba

我已经开始为我的考试学习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

Picture of the error

1 个答案:

答案 0 :(得分:3)

VBA非常以EN-US为中心,并且无论系统的区域设置使用什么(例如;)或Office安装所使用的语言,都希望使用英文函数名称和逗号作为列表分隔符。< / p>

要使用工作表中显示的公式,请使用Range.FormulaLocal propertyRange.FormulaR1C1Local property

'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.FormulaRange.FormulaLocal property

¹<子> SZUM függvény