Excel 多select,多列列表框
Excel multi-select, multi-column listboxes
我在编写一个用户窗体时遇到问题,该用户窗体从一个多列列表框中获取所选数据并将其添加到同一用户的另一个列表框中。添加后,数据从源列表框中移除
"ListBox" 是数据所在的位置,"listbox1" 是添加数据的位置。
Private Sub add_Click()
For i = 0 To ListBox.ListCount - 1
If ListBox.Selected(i) = True Then
NextEmpty = ListBox1.ListCount
ListBox1.List(NextEmpty, 0) = ListBox.List(i, 0)
ListBox1.List(NextEmpty, 1) = ListBox.List(i, 1)
ListBox1.List(NextEmpty, 2) = ListBox.List(i, 2)
ListBox.RemoveItem (i)
End If
Next
End Sub
此代码给我一个 运行 时间错误“381”
"Could not set the list property. Invalid property array index."
我环顾四周,但似乎无法确定如何正确使用这些属性。任何帮助是极大的赞赏。
您不能将值设置为大于列表中最大值的索引(并且最大值正好是 ListCount
(如果从零开始,则为 ListCount-1
)。
因此,您必须 add
具有 ListBox1.Add
的值
为了做到这一点,就像大牛说的,我们需要使用一个添加函数。
在下面的代码中,您可以看到我如何在 with 块中使用 .additem
函数。
要在将选择移动到新的列表框后删除选择,我 运行 向后循环。
For i = MainListBox.ListCount - 1 To 0 Step -1
Private Sub add_Click()
Dim i As Integer
For i = 0 To MainListBox.ListCount - 1
If MainListBox.Selected(i) Then
With ListBox1
.AddItem
.List(.ListCount - 1, 0) = MainListBox.List(i, 0)
.List(.ListCount - 1, 1) = MainListBox.List(i, 1)
.List(.ListCount - 1, 2) = MainListBox.List(i, 2)
End With
End If
Next i
For i = MainListBox.ListCount - 1 To 0 Step -1
If MainListBox.Selected(i) Then
MainListBox.RemoveItem (i)
End If
Next i
End Sub
我在编写一个用户窗体时遇到问题,该用户窗体从一个多列列表框中获取所选数据并将其添加到同一用户的另一个列表框中。添加后,数据从源列表框中移除
"ListBox" 是数据所在的位置,"listbox1" 是添加数据的位置。
Private Sub add_Click()
For i = 0 To ListBox.ListCount - 1
If ListBox.Selected(i) = True Then
NextEmpty = ListBox1.ListCount
ListBox1.List(NextEmpty, 0) = ListBox.List(i, 0)
ListBox1.List(NextEmpty, 1) = ListBox.List(i, 1)
ListBox1.List(NextEmpty, 2) = ListBox.List(i, 2)
ListBox.RemoveItem (i)
End If
Next
End Sub
此代码给我一个 运行 时间错误“381” "Could not set the list property. Invalid property array index." 我环顾四周,但似乎无法确定如何正确使用这些属性。任何帮助是极大的赞赏。
您不能将值设置为大于列表中最大值的索引(并且最大值正好是 ListCount
(如果从零开始,则为 ListCount-1
)。
因此,您必须 add
具有 ListBox1.Add
为了做到这一点,就像大牛说的,我们需要使用一个添加函数。
在下面的代码中,您可以看到我如何在 with 块中使用 .additem
函数。
要在将选择移动到新的列表框后删除选择,我 运行 向后循环。
For i = MainListBox.ListCount - 1 To 0 Step -1
Private Sub add_Click()
Dim i As Integer
For i = 0 To MainListBox.ListCount - 1
If MainListBox.Selected(i) Then
With ListBox1
.AddItem
.List(.ListCount - 1, 0) = MainListBox.List(i, 0)
.List(.ListCount - 1, 1) = MainListBox.List(i, 1)
.List(.ListCount - 1, 2) = MainListBox.List(i, 2)
End With
End If
Next i
For i = MainListBox.ListCount - 1 To 0 Step -1
If MainListBox.Selected(i) Then
MainListBox.RemoveItem (i)
End If
Next i
End Sub