尝试一个非常简单的和宏

时间:2015-10-22 18:04:14

标签: vba sum

我正在尝试执行一个简单的求和函数,它允许我事先定义范围,但我得到一个#NAME?错误。

Sub SumSelectedRange()
Dim ThisRng As Range
Set ThisRng = Application.InputBox("Select a range", "Get Range", Type:=8)
ActiveCell.Formula = "=SUM(ThisRng)"
End Sub

怎么了?

1 个答案:

答案 0 :(得分:0)

使用范围或任何其他vba引用时,它必须在引号旁边。

像这样:

Sub SumSelectedRange()
Dim ThisRng As Range
Set ThisRng = Application.InputBox("Select a range", "Get Range", Type:=8)
ActiveCell.Formula = "=SUM(" & ThisRng.Address & ")"
End Sub