Option
Explicit
Sub
Bsp01()
Dim
colEmployees
As
VBA.Collection
Dim
vntEmployee
As
Variant
Set
colEmployees =
New
VBA.Collection
AddEmployee01 colEmployees,
"Tobi"
, 1
AddEmployee01 colEmployees,
"Klaus"
, 0.82
AddEmployee01 colEmployees,
"Jasmin"
, 1.1
AddEmployee01 colEmployees,
"Karl"
, 0.4
AddEmployee01 colEmployees,
"Mia"
, 0.52
AddEmployee01 colEmployees,
"Jasmin"
, 0.75
For
Each
vntEmployee
In
colEmployees
Debug.Print Split(vntEmployee,
";"
)(0),
CSng
(Val(Split(vntEmployee,
";"
)(1)))
Next
End
Sub
Private
Sub
AddEmployee01(List
As
VBA.Collection, Name
As
String
, Hours
As
Single
)
Dim
v
As
Variant
On
Error
Resume
Next
v = Split(List(Name),
";"
)
v = Val(v(LBound(v) + 1))
Call
List.Remove(Name)
On
Error
GoTo
0
List.Add Key:=Name, _
Item:=Name &
";"
& Replace$(
CSng
(v) + Hours, Application.International(XlApplicationInternational.xlDecimalSeparator),
"."
, Count:=1)
End
Sub
Sub
Bsp02()
Dim
dicEmployees
As
Object
Dim
vntEmployees
As
Variant
Dim
i
As
Long
Set
dicEmployees = CreateObject(
"Scripting.Dictionary"
)
dicEmployees.CompareMode = vbTextCompare
AddEmployee02 dicEmployees,
"Tobi"
, 1
AddEmployee02 dicEmployees,
"Klaus"
, 0.82
AddEmployee02 dicEmployees,
"Jasmin"
, 1.1
AddEmployee02 dicEmployees,
"Karl"
, 0.4
AddEmployee02 dicEmployees,
"Mia"
, 0.52
AddEmployee02 dicEmployees,
"Jasmin"
, 0.75
vntEmployees = dicEmployees.Keys
For
i = LBound(vntEmployees)
To
UBound(vntEmployees)
Debug.Print vntEmployees(i), dicEmployees(vntEmployees(i))
Next
End
Sub
Private
Sub
AddEmployee02(Dictionary
As
Object
, Name
As
String
, Hours
As
Single
)
Dim
h
As
Single
If
Dictionary.Exists(Name)
Then
h = Dictionary(Name)
Call
Dictionary.Remove(Name)
End
If
Dictionary.Add Key:=Name, Item:=h + Hours
End
Sub