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

时间:2013-12-22 13:34:31

标签: excel vba excel-vba

这看起来似乎是一个非常简单的问题,但我无法弄清楚为什么我的函数在Range.value = val调用上意外结束。也许我错过了一些非常基本的东西,但我已经测试了这些,并且每个人都没有解决任何问题,我不知道如何捕获错误。

这是初始功能:

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

我在我的智慧结束。我搜索了MSDN,stackoverflow和所有excel论坛,所有这些都显示设置值到这样的范围。我甚至将范围值的设置分离为不同的函数,如下所示:

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

最终,我希望能够做一些像this article中所讨论的那样的事情,我会循环遍历各种各样的范围并增加它们。任何帮助都将非常感激。

1 个答案:

答案 0 :(得分:2)

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

如果从工作表单元格调用您的函数,那么它将在正确的行中“轰炸”。从单元格调用的UDF不能修改其他单元格的内容,它只能返回一个值。