Home > Enterprise >  VBA Compile error: Object required trying to set data into a class
VBA Compile error: Object required trying to set data into a class

Time:06-03

It has been a while since I have programmed in VBA. I wanted to clean up my code and make it a bit more readable to future me. So I started to make some classes. Here is my simplified problem:

'class code ' class is called cls_curriculum_object
Dim m_curriculum_object_name As String

Public Property Get curriculum_object_name()
    curriculum_object_name = m_curriculum_object_name
End Property

Public Property Set curriculum_object_name(curriculum_object_name)
    m_curriculum_object_name = curriculum_object_name
End Property


' button code
Dim cls_curriculum_object As cls_curriculum_object
Set cls_curriculum_object = New cls_curriculum_object

ActiveSheet.Cells(ActiveCell.Row, 4).Select
Set cls_curriculum_object.curriculum_object_name = Trim(ActiveCell.FormulaR1C1)

' if I run it. It works fine but if I change the code to
Set cls_curriculum_object.curriculum_object_name = "foo"
' I get a VBA compile error message

Thank you in advance.

CodePudding user response:

You have a String and need Property Let, not Property Set:

Public Property Let curriculum_object_name(curriculum_object_name)
    m_curriculum_object_name = curriculum_object_name
End Property

...

cls_curriculum_object.curriculum_object_name = Trim(ActiveCell.FormulaR1C1)
cls_curriculum_object.curriculum_object_name = "foo"

Side note, but snake_case is very unreadable.

  • Related