passing objects

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi,

do you pass an object to a function the same way as you
pass a normal variable.
ie, is this correct

public sub main()
anyVariable = anyFunction(rangeObject)
end sub

public Function anyFunction(rangeObject as range)
k = rangeObject.end(xldown).row
anyFunction=k
end Function
 
Hi John,

In essence what you are doing is correct. A couple of comments though.

You must declare the variable rangeObject, and declare it as type Range,
otherwise it will be implicitly declared as type Variant and you will get a
ByRef error when you try to pass it to the function where the argument is
specifically typed as a Range (declaring and typing variables is good
anyway).

Also, it is better IMO to declare the function type, and declare any
variables used within it, like so

Public Function anyFunction(rangeobject As Range) As Long
Dim k As Long
k = rangeobject.End(xlDown).Row
anyFunction = k
End Function

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top