lundi 30 mars 2015

Vba collection set and retrieve value from collection within custom function

Im trying loop over a cell range (single column) that contains names and codes containing the string "cgl".


I can loop over the range and identify the cells but i was trying to use a collection to store the name and the row number so i can return it. But i am not setting the value correctly and probably not retrieving the value correctly (the msg box command does not work after this point). I am using excel 2007.


I followed examples found in other q's, online help, but what is the correct way to use collection?



Function DHNameRows(rng As Range, name)
' eg name = david

Dim item As Range
Dim staff As Collection
Set staff = New Collection

For Each item In rng
found = InStr(item.Value, "CGL")
If found = 0 Then
'item value should equal david
MsgBox item.Value
staff.Add item.Value, item.Row
End If
Next
MsgBox staff.item(name)
DHNameRows = staff.item(name)

End Function

Aucun commentaire:

Enregistrer un commentaire