Excel VBA How To Sort Multi Column List Box With Condition

kashif.special2005

Active Member
Joined
Oct 26, 2009
Messages
443
Hi,

I have a form and in that form I have a list box with 4 columns Name, Age, Type and TransDate and I want to sort the list box by TransDate with condition.

Sort Condition:- Those TransDate is within before 24 hours by today's date will show first then show rest of the dates below.

Note 1:- In TransDate column there is also NULL.

Note 2:- In TransDate column Dates may also be futures date

ListBox Name is "QCList"

Please help me to resolve this problem.

Thanks
Kashif
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi,

I just build the logic to sort the list box with condition, and I just wanted to share the code.

Code:
Sub test()


    Dim vData As Variant
    
    vData = Me.ListBox1.List
    
    vData = SortByDate(vData)


    Me.ListBox1.List = vData


End Sub




Function SortByDate(vardata As Variant) As Variant




    Dim i As Long, iLastCount As Integer, iStartCount As Integer
    Dim iLastCol As Integer, iLoop As Integer
    Dim vMydata() As Variant, vMyDataNew() As Variant
    Dim dStart As Date, dEnd As Date
    
    dEnd = Date
    dStart = DateAdd("d", -1, dEnd)
    
    ReDim vMydata(LBound(vardata, 1) To UBound(vardata, 1), LBound(vardata, 2) To UBound(vardata, 2) + 1)
    
    For i = LBound(vardata, 1) To UBound(vardata, 1)
        For iLoop = LBound(vardata, 2) To UBound(vardata, 2)
            vMydata(i, iLoop) = vardata(i, iLoop)
        Next iLoop
    Next i
    
    iLastCol = UBound(vardata, 2) + 1
    
    For i = LBound(vardata, 1) To UBound(vardata, 1)
    
        If vardata(i, 3) >= dStart And vardata(i, 3) <= dEnd Then
            vMydata(i, iLastCol) = 1
        Else
            vMydata(i, iLastCol) = 2
        
        End If
    Next i


    ReDim vMyDataNew(LBound(vardata, 1) To UBound(vardata, 1), LBound(vardata, 1) To UBound(vardata, 2))


    iLastCount = UBound(vardata, 1)
    iStartCount = LBound(vardata, 1)


    For i = LBound(vMydata, 1) To UBound(vMydata, 1)
        If vMydata(i, 4) = 1 Then
            
            For iLoop = LBound(vMydata, 2) To UBound(vMydata, 2) - 1
                vMyDataNew(iStartCount, iLoop) = vMydata(i, iLoop)
            Next iLoop
            iStartCount = iStartCount + 1
        
        Else
            For iLoop = LBound(vMydata, 2) To UBound(vMydata, 2) - 1
                vMyDataNew(iLastCount, iLoop) = vMydata(i, iLoop)
            Next iLoop
            iLastCount = iLastCount - 1
        
        End If
    
    Next i


    SortByDate = vMyDataNew


End Function

Thanks
Kashif
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,876
Members
449,056
Latest member
ruhulaminappu

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