将项目从 Listbox1 移动到 Listbox2 代码
Moving items from Listbox1 to Listbox2 code
因此,Listbox1 已填充(来自另一个子,链接到工作簿中的单元格区域)并且用户可以 select(一次一个)列表框中的项目显示在 Listbox2 中,使用命令按钮(添加)来移动它们。我几乎设法让它变得完美。我的问题是,如果用户 仅 select 是 Listbox1 中的最后一个条目,则 Listbox1 中所有其他可能的条目都将被删除(空白 out/not visible/cannot被 selected).
我只想从 Listbox1 中删除 selected 的条目,因为它出现在 Listbox2 中。
代码如下:
Private Sub Add_Click()
Dim i as Integer
For i = 0 To Me.ListBox1.ListCount - 1
If Me.ListBox1.Selected(i) = True Then
Me.ListBox2.AddItem Me.ListBox1.List(i)
End If
Next i
For i = Me.ListBox1.ListCount - 1 To 0 Step -1
If Me.ListBox1.Selected(i) = True Then
Me.ListBox1.RemoveItem i
End If
Next
End Sub
如果这里有什么明显的地方,我真的很抱歉。但是这些列表框已经让我发疯了大约 3 天。
提前致谢
这是一个单一的 select 列表,所以一旦您点击 selected 项,请尝试在每个循环中添加 Exit For
:
Private Sub Add_Click()
Dim i as Long
For i = Me.ListBox1.ListCount - 1 To 0 Step -1
If Me.ListBox1.Selected(i) = True Then
Me.ListBox2.AddItem Me.ListBox1.List(i)
Me.ListBox1.RemoveItem i
Exit For
End If
Next
End Sub
因此,Listbox1 已填充(来自另一个子,链接到工作簿中的单元格区域)并且用户可以 select(一次一个)列表框中的项目显示在 Listbox2 中,使用命令按钮(添加)来移动它们。我几乎设法让它变得完美。我的问题是,如果用户 仅 select 是 Listbox1 中的最后一个条目,则 Listbox1 中所有其他可能的条目都将被删除(空白 out/not visible/cannot被 selected).
我只想从 Listbox1 中删除 selected 的条目,因为它出现在 Listbox2 中。
代码如下:
Private Sub Add_Click()
Dim i as Integer
For i = 0 To Me.ListBox1.ListCount - 1
If Me.ListBox1.Selected(i) = True Then
Me.ListBox2.AddItem Me.ListBox1.List(i)
End If
Next i
For i = Me.ListBox1.ListCount - 1 To 0 Step -1
If Me.ListBox1.Selected(i) = True Then
Me.ListBox1.RemoveItem i
End If
Next
End Sub
如果这里有什么明显的地方,我真的很抱歉。但是这些列表框已经让我发疯了大约 3 天。 提前致谢
这是一个单一的 select 列表,所以一旦您点击 selected 项,请尝试在每个循环中添加 Exit For
:
Private Sub Add_Click()
Dim i as Long
For i = Me.ListBox1.ListCount - 1 To 0 Step -1
If Me.ListBox1.Selected(i) = True Then
Me.ListBox2.AddItem Me.ListBox1.List(i)
Me.ListBox1.RemoveItem i
Exit For
End If
Next
End Sub