FILL combobox by combobox

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

FILL combobox by combobox

Post by sal21 »

Code: Select all

Private Sub SORT_DATE_COMBO()

With Me.CESERCIZIO

For I = 0 To .ListCount - 1

Debug.Print .List(I)

Next I

End With

End Sub

this loop return:

03/2020
05/2009
05/2018
06/2011
06/2015
06/2016
06/2018
08/2010
08/2015
10/2015
11/2008
11/2011
11/2013
11/2015
12/2005
12/2016

i need to fill a new combobox2, with a sorted date value...

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: FILL combobox by combobox

Post by HansV »

Code: Select all

Private Sub SORT_DATE_COMBO()
    Dim a() As String
    Dim i As Long
    Dim n As Long
    n = Me.CESERCIZIO.ListCount - 1
    ReDim a(n)
    For i = 0 To n
        a(i) = Me.CESERCIZIO.List(i)
    Next i
    a = SortAsDate(a)
    Me.ComboBox2.Clear
    For i = 0 To n
        Me.ComboBox2.AddItem a(i)
    Next i
End Sub

Function SortAsDate(ByVal a As Variant)
    Dim i As Long
    Dim j As Long
    Dim Tmp As Variant
    For i = LBound(a) To UBound(a) - 1
        For j = i + 1 To UBound(a)
            If DateSerial(Right(a(i), 4), Left(a(i), 2), 1) > DateSerial(Right(a(j), 4), Left(a(j), 2), 1) Then
                Tmp = a(i)
                a(i) = a(j)
                a(j) = Tmp
            End If
        Next j
    Next i
    SortAsDate = a
End Function
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

Re: FILL combobox by combobox

Post by sal21 »

HansV wrote:
27 Apr 2021, 08:25

Code: Select all

Private Sub SORT_DATE_COMBO()
    Dim a() As String
    Dim i As Long
    Dim n As Long
    n = Me.CESERCIZIO.ListCount - 1
    ReDim a(n)
    For i = 0 To n
        a(i) = Me.CESERCIZIO.List(i)
    Next i
    a = SortAsDate(a)
    Me.ComboBox2.Clear
    For i = 0 To n
        Me.ComboBox2.AddItem a(i)
    Next i
End Sub

Function SortAsDate(ByVal a As Variant)
    Dim i As Long
    Dim j As Long
    Dim Tmp As Variant
    For i = LBound(a) To UBound(a) - 1
        For j = i + 1 To UBound(a)
            If DateSerial(Right(a(i), 4), Left(a(i), 2), 1) > DateSerial(Right(a(j), 4), Left(a(j), 2), 1) Then
                Tmp = a(i)
                a(i) = a(j)
                a(j) = Tmp
            End If
        Next j
    Next i
    SortAsDate = a
End Function
THE BIG ONE! :grin:
WORK! :cheers:

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

Re: FILL combobox by combobox

Post by sal21 »

HansV wrote:
27 Apr 2021, 08:25

Code: Select all

Private Sub SORT_DATE_COMBO()
    Dim a() As String
    Dim i As Long
    Dim n As Long
    n = Me.CESERCIZIO.ListCount - 1
    ReDim a(n)
    For i = 0 To n
        a(i) = Me.CESERCIZIO.List(i)
    Next i
    a = SortAsDate(a)
    Me.ComboBox2.Clear
    For i = 0 To n
        Me.ComboBox2.AddItem a(i)
    Next i
End Sub

Function SortAsDate(ByVal a As Variant)
    Dim i As Long
    Dim j As Long
    Dim Tmp As Variant
    For i = LBound(a) To UBound(a) - 1
        For j = i + 1 To UBound(a)
            If DateSerial(Right(a(i), 4), Left(a(i), 2), 1) > DateSerial(Right(a(j), 4), Left(a(j), 2), 1) Then
                Tmp = a(i)
                a(i) = a(j)
                a(j) = Tmp
            End If
        Next j
    Next i
    SortAsDate = a
End Function
BRO, i need to use this sort code for a date in combo with this format dd/mm/yyyy, can you modify...

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: FILL combobox by combobox

Post by HansV »

Change the line

Code: Select all

           If DateSerial(Right(a(i), 4), Left(a(i), 2), 1) > DateSerial(Right(a(j), 4), Left(a(j), 2), 1) Then
to

Code: Select all

            If DateSerial(Right(a(i), 4), Mid(a(i), 4, 2), Left(a(i), 2)) > DateSerial(Right(a(j), 4), Mid(a(j), 4, 2), Left(a(j), 2)) Then
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

Re: FILL combobox by combobox

Post by sal21 »

:clapping:
HansV wrote:
04 May 2021, 11:04
Change the line

Code: Select all

           If DateSerial(Right(a(i), 4), Left(a(i), 2), 1) > DateSerial(Right(a(j), 4), Left(a(j), 2), 1) Then
to

Code: Select all

            If DateSerial(Right(a(i), 4), Mid(a(i), 4, 2), Left(a(i), 2)) > DateSerial(Right(a(j), 4), Mid(a(j), 4, 2), Left(a(j), 2)) Then
:clapping: :clapping: :clapping: