我想知道是否有一种简单的方法可以将 a 中的所有选定项目存储ListBox
到一个数组中。
我试图用下面的代码来做到这一点,但它没有工作除外。FilterTest()
什么都不给我。
Private Sub ListBox1_Change()
Dim FilterTest() As Variant
Dim myMsg As String
Dim i As Long
Dim Count As Integer
Count = 1
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) Then
ReDim Preserve FilterTest(Count)
FilterTest(Count) = ListBox1.List(i)
Count = Count + 1
End If
Next i
End Sub
你很接近。数组是基数为 0 的集合(除非另有说明),因此从 0 开始计数。您也可以在声明变量时去掉括号。接下来,第一次 redim 变量,然后 redim 在接下来的迭代中保留它。
Private Sub ListBox1_Change()
Dim FilterTest As Variant
Dim myMsg As String
Dim i As Long
Dim Count As Integer
Count = 0
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) Then
If Count = 0 Then
Redim FilterTest(Count)
Else
Redim Preserve FilterTest(Count)
End If
FilterTest(Count) = ListBox1.List(i)
Count = Count + 1
End If
Next i
End Sub
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句