好吧,这看起来很疯狂,我已经研究了几个小时,但我找不到任何有用的东西。这篇文章将完全没有代码,但我将很快解释我正在尝试做什么。
所以我有一个我已成功填充的列表框,它工作得很好。在用户指示的某个时刻,用户将从列表框中选择一行,将其称为RecordBox,查看一些信息,然后添加一些,然后单击“保存”命令按钮。单击此保存按钮后,我想将选定的行从RecordBox复制到第二个列表框。我想它叫做DetailsBox。
我要么需要一种方法来获取表单中显示的数据,以字幕,组合框条目和文本框条目的形式,向“DetailsBox”添加一行并将信息复制到该行的特定列,或者我需要简单地将选定的行从RecordBox复制到DetailsBox。
无论如何,如果某些代码有用,请问,但除了命令按钮单击事件之外,确实没有任何其他内容。
我希望这些信息足够了。
答案 0 :(得分:3)
就像
一样简单ListBox2.AddItem ListBox1.List(ListBox1.ListIndex)
关注(来自评论)
我想我会将该行发送到工作表,然后将其添加到其他列表框中。
我相信您使用的是多列列表框。在这种情况下,上面的代码只会将第一列添加到第二个列表框中。您需要遍历其余列以添加Listbox1
中的选定行。
假设您的用户形态如下所示。我为你创建了一个小样本。
并且列表框的属性设置如下
这就是你Sheet1
的样子。
现在将此代码用于Userform。
Private Sub UserForm_Initialize()
'~~> Adding Sample Data to listbox 1
ListBox1.List = ThisWorkbook.Sheets(1).Range("A1:E3").Value
End Sub
Private Sub CommandButton1_Click()
Dim iIndex
Dim i As Long, j As Long, k As Long
With ListBox1
i = .ListIndex
ListBox2.AddItem .List(i, 0), 0
j = ListBox2.ListCount - 1
For k = 1 To .ColumnCount - 1
ListBox2.List(j, k) = .List(i, k)
Next k
End With
End Sub
当您点击Listbox1
中的某个项目并按下命令按钮时,您会注意到Listbox1
中所选行已成功复制到Listbox2
答案 1 :(得分:0)
任何人都希望使用循环和多选来将项目从一个列表框发送到另一个列表框。下面是一些可能有用的代码。您需要将两个列表框的属性设置为Mulitselect。 1 fmMultiSelectMulti。然后使用与上面发布的Siddharth Rout相同的设置/设置。
Private Sub CommandButton1_Click()
Dim iIndex
Dim i As Long, j As Long, k As Long
ListBox2.Clear
For i = 0 To 2' loop 3 times for each row in listbox1.
If ListBox1.Selected(i) = True Then 'Get the first selected Row index number.
ListBox2.AddItem ListBox1.List(i, 0) 'Gets the first item from listbox1 and puts it in listbox2.
j = ListBox2.ListCount - 1 ' counts all items in listbox2. which is one item.
For k = 1 To ListBox1.ColumnCount - 1 'Count the columns listbox1.Now that the first item is in listbox2 _
move over one column & copy the next value to listbox2. loop 4 more times for 4th entry of row one.
ListBox2.List(j, k) = ListBox1.List(i, k)
Next k
End If
Next i
End Sub