VBA Compile error: Object required trying to set data into a class
VBA Compile error: Object required trying to set data into a class
我已经有一段时间没有在 VBA 中编程了。我想清理我的代码,让它对未来的我更具可读性。所以我开始做一些类。这是我的简化问题:
'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
提前致谢。
您有 String
并且需要 Property Let
,而不是 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"
旁注,但是 snake_case 非常难读。
我已经有一段时间没有在 VBA 中编程了。我想清理我的代码,让它对未来的我更具可读性。所以我开始做一些类。这是我的简化问题:
'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
提前致谢。
您有 String
并且需要 Property Let
,而不是 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"
旁注,但是 snake_case 非常难读。