8/12/2016 - 7:36 AM

Excel VBA Classes

Excel VBA Classes

'Private fields
Private strStuName As String
Private dblStuMarks As Double

'Let -> "Set" but for non object types
Public Property Let Name(strN As String)
  strStuName = strN
End Property

Public Property Get Name() As String
  Name = strStuName
End Property

'Set -> Set for object types
' The Pen property may be set to different Pen implementations. 
Property Set Pen(P As Object) 
 Set CurrentPen = P ' Assign Pen to object. 
End Property
Use the Dim statement to create a variable and define it as a reference to the class:

Dim student As ClsStudent

Create a new object reference using the New keyword:

Set student = New ClsStudent

Public Function Grade() As String

  Dim strGrade As String
  'do something here
  'then return Grade
  Grade = strGrade

End Function
Insert a class module(as opposed to just "module") in the project explorer.

Rename the class in the Properties Window in VBE.

Instance property: Determine if it is allowed for external prjects from working with and using instances of that class.