【问题标题】:Sorting dates using vba使用 vba 对日期进行排序
【发布时间】:2020-08-20 19:04:52
【问题描述】:

我在列表框中显示了一个数据列表,单击一个按钮后,该列表出现在我的用户窗体上。 我的列表第 2 列有日期,我想进行降序排序。 我有下面的代码,但它不起作用,我错了吗?

    fin_col_Form_Init = Ws.Cells(6, 256).End(xlToLeft).Column
    UF_Profil_Edit1.ListBox_Form_Init.ColumnCount = 2
    UF_Profil_Edit1.ListBox_Form_Init.ColumnWidths = "300;100"
    fin_col_Form_Init = Ws.Cells(6, 256).End(xlToLeft).Column
UF_Profil_Edit1.ListBox_Form_Init.ColumnCount = 2
UF_Profil_Edit1.ListBox_Form_Init.ColumnWidths = "300;100"

For i = 2 To fin_col_Form_Init
UF_Profil_Edit1.ListBox_Form_Init.AddItem Ws.Cells(6, i)
UF_Profil_Edit1.ListBox_Form_Init.List(UF_Profil_Edit1.ListBox_Form_Init.ListCount - 1, 1) = Ws.Cells(7, i)
Next i

    Dim y, x As Integer
    Dim MyList As Variant
    
        With UF_Profil_Edit1.ListBox_Form_Init
            For y = 0 To .ListCount - 1
                For x = y To .ListCount - 1
                    If CDate(.List(x, 1)) > CDate(.List(y, 1)) Then
                        For c = 0 To 2
                            MyList = .List(x, c)
                            .List(x, c) = .List(y, c)
                            .List(y, c) = MyList
                        Next c
                    End If
                Next x
    
            .List(y, 2) = Format(.List(y, 2), "####.00")
            Next y
        End With

【问题讨论】:

  • a) 请描述“不工作” - b) 您是否在调试器中单步执行您的代码并查看它所采取的步骤和变量的值?你学到了什么,你的代码哪里出错了?
  • 你应该从列表中提取一个数组,对数组进行排序,然后使用它加载回列表框(排序)...
  • @braX 我写的代码很好,它只显示元素列表而不对它们进行排序,就像排序代码不存在
  • @braX 我的代码正在运行,我刚刚注意到我将代码放在了错误的位置(在加载我的数据之前),显然如果没有数据,代码想要进行排序在名单上。我的错。非常感谢

标签: excel vba


【解决方案1】:

请尝试下一个代码:

Sub testSortListBox()
  Dim i As Long, j As Long, sTemp As Date, sTemp2 As String, SortList As Variant
    
    UF_Profil_Edit1.ListBox_Form_Init.ColumnCount = 2
    UF_Profil_Edit1.ListBox_Form_Init.ColumnWidths = "300;100"

    'Store the list in an array to be sorted:
    SortList = UF_Profil_Edit1.ListBox_Form_Init.List
    
    'Sort the array on the second column
    For i = LBound(SortList, 1) To UBound(SortList, 1) - 1
        For j = i + 1 To UBound(SortList, 1)
            If CDate(SortList(i, 1)) < CDate(SortList(j, 1)) Then
                'Swap the second value
                sTemp = SortList(i, 1)
                SortList(i, 1) = SortList(j, 1)
                SortList(j, 1) = sTemp
                
                'Swap the first value
                sTemp2 = SortList(i, 0)
                SortList(i, 0) = SortList(j, 0)
                SortList(j, 0) = sTemp2
            End If
        Next j
    Next i
    
    'Remove the contents of the listbox:
    UF_Profil_Edit1.ListBox_Form_Init.Clear
    'Load the sorted array in the list box:
    UF_Profil_Edit1.ListBox_Form_Init.List = SortList
End Sub

但是,请注意:讨论中的列表框不能链接到范围(不是通过其RowSource 属性加载...

【讨论】:

  • 我试过你的代码,列表框是空的,我的列表里什么都没有。
  • 但是,你想排序什么?您没有“出现在您的用户表单上”的列表框(ListBox_Form_Init)吗?你是什​​么意思?列表已填满,使用我上面的代码排序后,它似乎是空的?
  • 它现在可以工作了,对不起我有点累了我把代码复制到了错误的地方,应该在加载数据列表后添加我在加载数据之前放了代码,现在是在职的。非常感谢。
  • @IMP_Te:那么,它回答了你的问题吗?如果是,我们在这里勾选代码左侧的复选框,以使其接受答案。这样,搜索类似问题的其他人就会知道该代码有效...
猜你喜欢
  • 2012-12-08
  • 2015-12-30
  • 1970-01-01
  • 1970-01-01
  • 2020-10-30
  • 2015-04-01
  • 2016-03-23
  • 2013-07-20
相关资源
最近更新 更多