Problem passing a range as argument

Hello there,

I'm developing a macro do do some calculations on a worksheet, but I always get a problem when I pass a Range as an argument to my function.

My code is simple, and here it is:

Sub Main()
    Dim Range_do_Comeco As Range
    Set Range_do_Comeco = Worksheets("Plan1").Range("AD2")
    MyFunc(Range_do_Comeco )
End Sub

Sub MyFunc(ByRef Valor_do_Range As Range)
'etc etc

End Sub

I need to pass the range as argument, because I change the values around the "area" of the range, and it keeps going throught the Plan.

The compiler keeps saying, when I call MyFunc that "The object is necessary". Dunno what's wrong with the code =/

Thanks in advance if someone can help me =], and sorry for the bad english.



Answer this question

Problem passing a range as argument

  • CommonMan53

    Thx Andy, that helped a lot =]

    Now I can do a proper calling of Subs ^^


  • AshishGupta

    I've found the solution.

    The correct way to call the Sub is like that:

    MySub Valor_do_Range :=Range_do_Comeco

    Note the := , not = that's why it's working =D

    Just a brute force method, I still don't know why this works, hehe. Just compared with some others macros around here.

    Cya!


  • Ted Strom

    Hi,

    When passing arguments to a subroutine you do not need the brackets, unless you are using the Call method.

    Call MySub(Range_do_Comeco)

    the := is used when naming arguments. This allows you to pass arguments in a order that is different from the routines signature.


    Example:

    Sub Test()

    MySub Arg2:="World", Arg1:="Hello"

    Call MySub("Hello", "World")

    MySub "Hello", "World"

    End Sub

    Sub MySub(Arg1 As String, Arg2 As String)

    MsgBox Arg1 & " " & Arg2

    End Sub




  • Problem passing a range as argument