VBA Range.value函数导致意外的结束

这似乎是一个非常简单的问题,但我不明白为什么我的函数在Range.value = val调用意外结束。 也许我错过了一些非常基本的东西,但我已经testing了这些东西,并且他们每个人都没有解决任何问题,我不知道如何捕捉错误。

这是最初的function:

Function incrementCount(upper As Range, Summed As Range, ParamArray sums() As Variant) Dim deduct As Integer Dim summation As Integer Dim elem As Variant Dim i As Long Dim temp As Range up = upper.Value summation = Summed.Value 'Initialize the starting points of the increments For i = LBound(sums) To UBound(sums) MsgBox IsObject(sums(i)) 'Prints out as an true MsgBox TypeName(sums(i)) 'Prints out as Rnage MsgBox sums(i).Value 'Prints out as 0 Set temp = sums(i) MsgBox temp.Value 'Prints out as 0 Set temp = Summed MsgBox temp.Value 'Prints out as 1 (which is correct) temp.value = 3 'Errors here MsgBox temp.Value 'Never makes it to this line sums(i).Value = 1 'I have also tried this with the same result Next i <more code that is never reached> End Function 

我不知道该怎么做。 我已经search了MSDN,stackoverflow,和所有的许多Excel论坛,他们都显示设置值这样的范围。 我甚至将范围值的设置分隔到不同的function,如下所示:

 Function testsub(thecell As Range, thevalue As Integer) thecell.value = thevalue End Function 

最终,我希望能够做一些像本文中讨论的东西,我循环了一个随机分类的范围,并将增加它们。 任何帮助都将不胜感激。

您尚未指定如何调用IncrementCount()

如果你的函数是从工作表单元格中调用的,那么它就是在正确的线路上“轰炸”出来的。 从单元格调用的UDF不能修改其他单元格的内容,它只能返回一个值。