How class definitions work in VBA

As I try to learn VBA, classes blast me with heavy pain.

Here is something from one of my VBA books (VBA and Macros Microsoft Excel 2010 - Bill Jelen on page 497), which is completely unusual and unusual:

  • We open the class module in VBE

  • And we write 4 properties and 1 method of the user object in the class module clsEmployee:

    'Properites   
    Public EmpName As String             
    Public EmpID As String   
    Public EmpRate As Double         
    Public EmpWeeklyHrs As Double
    
    'Methods   
    Public Function EmpWeeklyPay() As Double  
        EmpWeeklyPay = EmpRate * EmpWeeklyHrs  
    End Function
    
  • The book says that the user object is completed

  • Now the book opens a simple module (and not a class module) for referencing a user object from another module, so it goes:

    Dim Employee as clsEmployee
    
    Set Employee = New clsEmployee
    
  • The entire code block is not displayed. The really inconvenient thing is that Dim Employee as clsEmployee is outside the routine! Why? (Thus, this can be seen as a secondary issue)

    Option Explicit
    
    Dim Employee as clsEmployee 'why is this outside of the code block?
    
    Sub EmpPay()
        Set Employee = New clsEmployee
    
        With Employee
            .EmpName = "Tracy Syrstad"
            .EmpID = "1651"
            .EmpRage = 25
            .EmpWeeklyHrs = 40
    
            MsgBox .EmpName & " earns $" & .EmpWeeklyPay & " per week."
        End With
    
    End Sub
    
  • The book kindly says:

Employee clsEmployee. , . EmpWeeklyPay, .

, ( VBA-, ), , ,   Application.Name
... VBA String, . , VBA - , . , ,

Public EmpName As String
Public EmpID As String
Public EmpRate As Double
Public EmpWeeklyHrs As Double

... , Name Value, 4 ? , ? EmpName EmpID. , , , . , ? - , , , , , /set EmpID, , . , EmpRate, EmpWeeklyHrs , "" , , , - , Value Name , ?

+4
1

, , :

:

Dim Employee as clsEmployee ' ?

, Employee . . . , , . . , . : Visual Basic .

:

, , EmpRate, EmpWeeklyHrs , "", , .

. , Assembler. Employee.EmpRate, , Employee.EmpRate, , Employee.EmpRate Employee.EmpWeeklyHrs. / , , 2 .

, ( , , ): enter image description here ,

Doc = Employee.EmpName 

VBA , "Employee", , "Name" 8 1000 . "" 1008 Doc.

+4

Source: https://habr.com/ru/post/1524879/


All Articles