Collections

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,834
Office Version
  1. 365
  2. 2019
Platform
  1. 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:

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.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Tired,

Step it through the code with a watch on colEmployees and the entire collection takes on the value of the last recEmployee added. Now I fully admit that class mods are new to me, but this looks like a ByRef vs. ByVal thing. That is to say, I would expect the .Add to put in the collect data "ByVal", but it changes everyone as if the collection is "ByRef".

Anyone have insight?

Tubal
 
Upvote 0
Hi,

There seems to be a line missing:

Rich (BB code):
Option Explicit

Sub EmpPayCollection()
    Dim colEmployees As New Collection
    Dim recEmployee As 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)
        Set recEmployee = New ClsEmployee
        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(1).EmpName
    
    Set recEmployee = Nothing
End Sub

Also, you don't need the "New" in the Dim.

Basically, ClsEmployee is a template. Creating a new one and calling it recEmployee makes a version of it available in the macro. If you change that one then you just overwrite. What you need to do is create a second instance for the second piece of data. The collection records all the instances.
 
Upvote 0
Thanks.

The code was taken from the book, so I suppose they got it wrong.
 
Upvote 0
Hi,

All good books should have the occasional error to keep the readers on their toes :)
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,678
Members
449,116
Latest member
HypnoFant

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top