在VBA Excel中传递范围对象

时间:2013-11-06 12:05:31

标签: excel-vba vba excel

这必须是例行的n00b问题。我从Test Sub调用myTest Sub,但只调用带有完整写出的参数的调用。当我将东西作为参数传递时(即myTest(stuff)),我得到了对象所需的错误。

将非常感激地获得解决我的谜题的任何帮助

Public Sub myTest(Arg1 As Range)
Dim elem As Variant
For Each elem In Arg1
    MsgBox (elem.Value)
Next elem
End Sub

Public Sub Test()
Dim stuff As Range
Set stuff = Worksheets(1).Range("D7:D10")
myTest (Worksheets(1).Range("D7:D10"))
'myTest (stuff)

End Sub

1 个答案:

答案 0 :(得分:0)

删除功能输入周围的括号,如下所示:

Public Sub myTest(Arg1 As Range)
Dim elem As Variant
For Each elem In Arg1
    MsgBox (elem.Value)
Next elem
End Sub

Public Sub Test()
Dim stuff As Range
Set stuff = Worksheets(1).Range("D7:D10")
myTest stuff

End Sub

现在应该可以了。