ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Passing Collection to function (https://www.excelbanter.com/excel-programming/408931-passing-collection-function.html)

Nigel RS[_2_]

Passing Collection to function
 
I have a set of new collections used to store lists of data. I now wish to
sort these collections in order and have a developed a general purpose
function to do just that. However I wish to pass the name of the collection
to my sort routine and I get an error "Argument not optional" as if the name
of the collection is not recognised as a collection.

Basic code structure as follows

' main code
Dim cA as new collection
Dim cB as new collection

' code that fills collection removed (that works ok)

Call SortCollection(cA) '<--- this line fails, although cA is a collection
and contains data

End main code


' SORT FUNCTION
Function SortCollection(mCol As Collection)
' sort the collection into order
Dim i As Long, j As Long
For i = 1 To mCol.Count - 1
For j = i + 1 To mCol.Count
If mCol(i) mCol(j) Then
Swap1 = mCol(i)
Swap2 = mCol(j)
mCol.Add Swap1, befo=j
mCol.Add Swap2, befo=i
mCol.Remove i + 1
mCol.Remove j + 1
End If
Next j
Next i
End Function



Nigel RS[_2_]

Passing Collection to function
 
Sorry everyone, I made the mistake...

Call SortCollection(cA)

or

SortCollection cA

Work OK!




"Nigel RS" wrote:

I have a set of new collections used to store lists of data. I now wish to
sort these collections in order and have a developed a general purpose
function to do just that. However I wish to pass the name of the collection
to my sort routine and I get an error "Argument not optional" as if the name
of the collection is not recognised as a collection.

Basic code structure as follows

' main code
Dim cA as new collection
Dim cB as new collection

' code that fills collection removed (that works ok)

Call SortCollection(cA) '<--- this line fails, although cA is a collection
and contains data

End main code


' SORT FUNCTION
Function SortCollection(mCol As Collection)
' sort the collection into order
Dim i As Long, j As Long
For i = 1 To mCol.Count - 1
For j = i + 1 To mCol.Count
If mCol(i) mCol(j) Then
Swap1 = mCol(i)
Swap2 = mCol(j)
mCol.Add Swap1, befo=j
mCol.Add Swap2, befo=i
mCol.Remove i + 1
mCol.Remove j + 1
End If
Next j
Next i
End Function




All times are GMT +1. The time now is 06:11 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com