?
Solved

VBA Collections

Posted on 2010-04-10
1
Medium Priority
?
546 Views
Last Modified: 2012-05-09
Hi,

I found some code (bottom of post) that sorts a collection by the field I request which works perfectly. The question I have is how to do this for two properties, i.e. by date then time?

Any help would be great.

Thanks

Mark
Private Function SortCollection(Col As Collection, psSortPropertyName As String, _
                                            pbAscending As Boolean, Optional psKeyPropertyName As String) As Collection

Dim obj As Object
Dim i As Integer
Dim j As Integer
Dim iMinMaxIndex As Integer
Dim vMinMax As Variant
Dim vValue As Variant
Dim bSortCondition As Boolean
Dim bUseKey As Boolean
Dim sKey As String
   
    bUseKey = (psKeyPropertyName <> "")
   
    For i = 1 To Col.Count - 1
        Set obj = Col(i)
        vMinMax = CallByName(obj, psSortPropertyName, VbGet)
        iMinMaxIndex = i
       
        For j = i + 1 To Col.Count
            Set obj = Col(j)
            vValue = CallByName(obj, psSortPropertyName, VbGet)
           
            If (pbAscending) Then
                bSortCondition = (vValue < vMinMax)
            Else
                bSortCondition = (vValue > vMinMax)
            End If
           
            If (bSortCondition) Then
                vMinMax = vValue
                iMinMaxIndex = j
            End If
           
            Set obj = Nothing
        Next j
       
        If (iMinMaxIndex <> i) Then
            Set obj = Col(iMinMaxIndex)
           
            Col.Remove iMinMaxIndex
            If (bUseKey) Then
                sKey = CStr(CallByName(obj, psKeyPropertyName, VbGet))
                Col.Add obj, sKey, i
            Else
                Col.Add obj, , i
            End If
           
            Set obj = Nothing
        End If
       
        Set obj = Nothing
    Next i
   
    Set SortCollection = Col
     
End Function
0
Comment
Question by:mcs26
1 Comment
 
LVL 15

Accepted Solution

by:
ChloesDad earned 2000 total points
ID: 30317270
Hi,

Assuming that you add a second parameter psSortPropertyName2 then you can extend your check
I dont have VBA to be able to check the syntax, but it will give you the basis of what you need to do

 For i = 1 To Col.Count - 1
        Set obj = Col(i)
        vMinMax = CallByName(obj, psSortPropertyName, VbGet)
        vMinMax2 = CallByName(obj, psSortPropertyName2, VbGet)

        iMinMaxIndex = i
       
        For j = i + 1 To Col.Count
            Set obj = Col(j)
            vValue = CallByName(obj, psSortPropertyName, VbGet)
            vValue2 = CallbyName(obj,psSortPropertyName2,vbGet)
           
            If (pbAscending) Then
                IF vValue < vMinMax then
                   bSortCondition = TRUE
                ELSE IF vValue = vMINMax THEN
                   IF vValue2 < vMinMax2 then
                      bSortCondition = TRUE
                   ELSE
                      bSortCondition = FALSE
                   END IF
                ELSE
                     bSortCondition = FALSE
                END IF          
            Else
                IF vValue > vMinMax then
                   bSortCondition = TRUE
                ELSE IF vValue = vMINMax THEN
                   IF vValue2 > vMinMax2 then
                      bSortCondition = TRUE
                   ELSE
                      bSortCondition = FALSE
                   END IF
                ELSE
                     bSortCondition = FALSE
                END IF          
            End If
           
            If (bSortCondition) Then
                vMinMax = vValue
                vMinMax2 = vValue2
                 iMinMaxIndex = j
            End If
           
            Set obj = Nothing
        Next j
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question