Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing object to Class method: Object doesn't support this property or method

Hi i'm working in excel 2013 vba, I want to pass a worksheet object to a class function:

'Class module Class1'

Sub receive(ByRef ws As Worksheet)
    msgbox ws.name
End Sub

Calling the class method receive with:

Sub passToClass()
  Dim ws as Worksheet
  Set ws = ThisWorkbook.Worksheets("sheet1")
  Dim myClass As New Class1
  myClass.receive(ws) ' Run-time Error '438': Object doesn't support this property or method
End Sub

A simple test that does work are two subroutines in the same module:

Sub pass()
   Dim ws as Worksheet
   Set ws = ThisWorkbook.Worksheets("sheet1")
   call receive(ws)
End Sub

Sub Receive(ByRef ws As Worksheet)
    msgbox ws.name
End Sub

What am I missing conceptually?

Thanks in advance!

like image 564
Aditya Avatar asked Jun 04 '15 07:06

Aditya


People also ask

How do you fix an object doesn't support the property or method?

Use the Object Browser to determine what members (properties and methods) are available for the selected class (object). Use the IntelliSense feature in the Visual Basic Editor. When you type a period (.) after a property or method in the Visual Basic Editor, a list of available properties and methods is displayed.


1 Answers

You need to remove the parentheses from the call:

myClass.receive ws

or add the Call keyword:

Call myClass.receive(ws)

Parentheses are only required when calling a routine if:

  • You use the Call keyword; or
  • You are calling a function and using the return value; or
  • You explicitly need to evaluate, or de-reference, the variable you are passing.
like image 68
Rory Avatar answered Oct 19 '22 15:10

Rory