Home » excel » excel vba – VBA class module: get property from an object inside the class

excel vba – VBA class module: get property from an object inside the class

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a class module containing some properties and control elements. How can I get property value of those controls in class level events? I tried

me.property 

or

control.parent.property 

but not worked. I think there should be a way to address properties of each instance of the class module inside the module, is’t it?

My Class Module: Cworker

Public WithEvents img1 As MSForms.Image
Private AcnS
Dim rng1 As Range, rng2 As Range, rng3 As Range

' Property Definitions

Public Property Get Aircraft() As String
    Aircraft = Acn
End Property
Public Property Let Aircraft(Value As String)
    Acn = Value
End Property

' Events Procedures

Private Sub img1_Click()  ' Showing Progress Form
    With ThisWorkbook.Sheets("Performance")
        Set rng1 = .Range("ProgCrit")
        Set rng2 = .Range("Progress")
        Set rng3 = .Range("op_log")
    End With
    frmprogress.LAc = Me.Aircraft 
    rng1.Cells(2, 1) = Me.Aircraft 
    rng3.AdvancedFilter xlFilterCopy, rng1, rng2
    oprecord.Calculate
    frmprogress.Show
End Sub

Tahnks,
M

How to&Answers:

I think you have a Typo on the second line:
I think and as per my test, you can refer to the me.property, property itself without the prefix, and also by the class variable you specified. You also can try them as well.

Public WithEvents img1 As MSForms.Image
Private Acn$ ' As String if you meant!
Dim rng1 As Range, rng2 As Range, rng3 As Range

' Property Definitions

Public Property Get Aircraft() As String
    Aircraft = Acn
End Property
Public Property Let Aircraft(Value As String)
    Acn = Value
End Property

' Events Procedures

Private Sub img1_Click()  ' Showing Progress Form
    With ThisWorkbook.Sheets("Performance")
        Set rng1 = .Range("ProgCrit")
        Set rng2 = .Range("Progress")
        Set rng3 = .Range("op_log")
    End With
    frmprogress.LAc = Me.Aircraft 
    rng1.Cells(2, 1) = Me.Aircraft 
    rng3.AdvancedFilter xlFilterCopy, rng1, rng2
    oprecord.Calculate
    frmprogress.Show
End Sub