tiredofit
Well-known Member
- Joined
- Apr 11, 2013
- Messages
- 1,937
- Office Version
- 365
- 2019
- Platform
- Windows
This code is taken from the book, VBA and Macros For Excel 2003.
Put a,b,c,d in column A, 1,2,3,4 in column B, 10,20,30,40 in column C and 5,6,7,8 in column D.
Standard module:
ClsEmployee
What I can't work out is why running the code, the message box shows:
even if I change this line to:
or any other number.
Put a,b,c,d in column A, 1,2,3,4 in column B, 10,20,30,40 in column C and 5,6,7,8 in column D.
Standard module:
Rich (BB code):
Sub EmpPayCollection()
Dim colEmployees As New Collection
Dim recEmployee As New ClsEmployee
Dim LastRow As Integer, myCount As Integer
Dim EmpArray As Variant
LastRow = ActiveSheet.Range("A65536").End(xlUp).Row
EmpArray = ActiveSheet.Range(Cells(1, 1), Cells(LastRow, 4))
For myCount = 1 To UBound(EmpArray)
With recEmployee
.EmpName = EmpArray(myCount, 1)
.EmpID = EmpArray(myCount, 2)
.EmpRate = EmpArray(myCount, 3)
.EmpWeeklyHrs = EmpArray(myCount, 4)
colEmployees.Add recEmployee, .EmpID
End With
Next myCount
MsgBox "Number of Employees: " & colEmployees.Count & Chr(10) & "Employee(2) Name: " & colEmployees(2).EmpName
Set recEmployee = Nothing
End Sub
ClsEmployee
Rich (BB code):
Option Explicit
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 EmpWeeklyHrs() As Double
EmpWeeklyHrs = NormalHrs + OverHrs
End Property
Property Get EmpNormalHrs() As Double
EmpNormalHrs = NormalHrs
End Property
Property Get EmpOverTimeHrs() As Double
EmpOverTimeHrs = OverHrs
End Property
Public Function EmpWeeklyPay() As Double
EmpWeeklyPay = (EmpNormalHrs * EmpRate) + (EmpOverTimeHrs * EmpRate * 1.5)
End Function
What I can't work out is why running the code, the message box shows:
Rich (BB code):
Employee(2) Name: d
even if I change this line to:
Rich (BB code):
MsgBox "Number of Employees: " & colEmployees.Count & Chr(10) & "Employee(2) Name: " & colEmployees(1).EmpName
or any other number.