传递Range参数时出现错误424

时间:2013-12-25 20:46:30

标签: excel excel-vba vba

我是VBA的新手,我在这个非常简单的代码中遇到错误:

Sub sub1()
    Dim myCell As Range
    Set myCell = Sheet1.Cells(2, 2)
    sub2 (myCell)      '<<<<------- Error 424
End Sub

Sub sub2(x As Range)
    'not doing anything yet
End Sub

我从Error 424调用sub2时收到sub1。在我看来,这应该有效,因为myCell是一个范围...任何想法?

1 个答案:

答案 0 :(得分:3)

删除括号,或添加Call

Sub Opt1()
    Dim myCell As Range
    Set myCell = Sheet1.Cells(2, 2)
    sub2 myCell     
End Sub

Sub sub2(x As Range)
MsgBox x.Address
End Sub

Sub Opt2()
    Dim myCell As Range
    Set myCell = Sheet1.Cells(2, 2)
    Call sub2(myCell)
End Sub

Sub sub2(x As Range)
MsgBox x.Address
End Sub