【发布时间】:2015-04-15 05:20:38
【问题描述】:
我有一个工作簿,其中有一个列表框,人们可以选择多行。我想在选择后将这些信息以新的形式带到另一个 ListBox 中,一旦选择就会弹出。
我发现了在同一用户表单中的列表框之间传输的示例,但我需要帮助在不同表单之间移动它们。
这段代码展示了如何在同一个表单上的两个用户表单之间传递信息。但我需要修改它以在两种不同的形式之间移动它。就像您单击提交按钮并弹出另一个带有文本框的列表框一样。
Option Explicit
'Move Listbox Items in UserForm
'code from Dave Peterson
'posted on www.contextures.com
Private Sub BTN_moveAllLeft_Click()
Dim iCtr As Long
For iCtr = 0 To Me.ListBox2.ListCount - 1
Me.ListBox1.AddItem Me.ListBox2.List(iCtr)
Next iCtr
Me.ListBox2.Clear
End Sub
Private Sub BTN_moveAllRight_Click()
Dim iCtr As Long
For iCtr = 0 To Me.ListBox1.ListCount - 1
Me.ListBox2.AddItem Me.ListBox1.List(iCtr)
Next iCtr
Me.ListBox1.Clear
End Sub
Private Sub BTN_MoveSelectedLeft_Click()
Dim iCtr As Long
For iCtr = 0 To Me.ListBox2.ListCount - 1
If Me.ListBox2.Selected(iCtr) = True Then
Me.ListBox1.AddItem Me.ListBox2.List(iCtr)
End If
Next iCtr
For iCtr = Me.ListBox2.ListCount - 1 To 0 Step -1
If Me.ListBox2.Selected(iCtr) = True Then
Me.ListBox2.RemoveItem iCtr
End If
Next iCtr
End Sub
Private Sub BTN_MoveSelectedRight_Click()
Dim iCtr As Long
For iCtr = 0 To Me.ListBox1.ListCount - 1
If Me.ListBox1.Selected(iCtr) = True Then
Me.ListBox2.AddItem Me.ListBox1.List(iCtr)
End If
Next iCtr
For iCtr = Me.ListBox1.ListCount - 1 To 0 Step -1
If Me.ListBox1.Selected(iCtr) = True Then
Me.ListBox1.RemoveItem iCtr
End If
Next iCtr
End Sub
Private Sub cmdOK_Click()
Unload Me
End Sub
Private Sub UserForm_Initialize()
Dim iCtr As Long
With Me.ListBox1
For iCtr = 1 To 10
.AddItem "This is a test" & iCtr
Next iCtr
End With
With Me.ListBox2
For iCtr = 1 To 10
.AddItem "This is a not a test" & iCtr
Next iCtr
End With
Me.ListBox1.MultiSelect = fmMultiSelectMulti
Me.ListBox2.MultiSelect = fmMultiSelectMulti
End Sub
【问题讨论】: