【发布时间】: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 我的代码正在运行,我刚刚注意到我将代码放在了错误的位置(在加载我的数据之前),显然如果没有数据,代码想要进行排序在名单上。我的错。非常感谢