I am working though the Creating Classes, records and collections chapter in Bill's book VBA and macros for excel.
I placed the following code in my non class module.
I have my locals windows open and can see that when a new record is added to colEmployees all the items in the collection are up to that record?
the data is in a sheet named "EmpHrs" headers in row 1
Can anyone help me out with this?
My class module
Debug window
Jack 100 10 40
10
Jill 101 20 10
20
monkey 102 30 35
30
see 103 40 45
40
I placed the following code in my non class module.
I have my locals windows open and can see that when a new record is added to colEmployees all the items in the collection are up to that record?
the data is in a sheet named "EmpHrs" headers in row 1
Can anyone help me out with this?
Code:
Sub EmpCollection()
Dim colEmployees As New Collection
Dim recEmployee As New clsEmployee
Dim lrEmpHr As Double
Dim arrEmp As Variant
Dim I As Integer
Dim strKey As String
lrEmpHr = shEmpHrs.Cells(Rows.Count, 1).End(xlUp).Row
arrEmp = shEmpHrs.Range(Cells(2, 1), Cells(lrEmpHr, 4))
For I = 1 To UBound(arrEmp)
With recEmployee
.EmpName = arrEmp(I, 1)
.EmpID = arrEmp(I, 2)
.EmpRate = arrEmp(I, 3)
.EmpWeeklyHrs = arrEmp(I, 4)
strKey = .EmpID
colEmployees.Add recEmployee, Key:=strKey
End With
Debug.Print arrEmp(I, 1), arrEmp(I, 2), arrEmp(I, 3), arrEmp(I, 4)
Debug.Print colEmployees(1).EmpRate
Next I
MsgBox "Number of Emloyees: " & colEmployees.Count & vbCrLf & "Employee Name: " & colEmployees.Item("101").EmpName
MsgBox "Jill's pay: $" & colEmployees("101").EmpWeeklyPay
Set recEmployee = Nothing
End Sub
**********************
Code:
Public EmpName As String
Public EmpID As String
Public EmpRate As Double
Private NormalHrs As Double
Private OverHrs As Double
Property Let EmpWeeklyHrs(Hrs As Double)
NormalHrs = WorksheetFunction.Min(40, Hrs)
OverHrs = WorksheetFunction.Max(0, Hrs - 40)
End Property
Property Get empweelyhrs() As Double
EmpWeeklyHrs = NormalHrs + OverHrs
End Property
Property Get EmpNormalhrs() As Double
EmpNormalhrs = NormalHrs
End Property
Property Get EmpOverHrs() As Double
EmpOverHrs = OverHrs
End Property
Public Function EmpWeeklyPay() As Double
EmpWeeklyPay = (EmpNormalhrs * EmpRate) + (EmpOverHrs * EmpRate * 1.5)
End Function
Jack 100 10 40
10
Jill 101 20 10
20
monkey 102 30 35
30
see 103 40 45
40
Last edited by a moderator: