VBA 列表框复制到列表框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19064043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 23:32:39  来源:igfitidea点击:

VBA listbox copy to listbox

vbalistboxcopycommandbutton

提问by user2759242

Ok, this seems nuts and I've been researching it for a couple of hours but I can't find anything that works. This post is going to be pretty devoid of code, but I'm going to explain very quickly exactly what I'm trying to do.

好吧,这似乎很疯狂,我已经研究了几个小时,但找不到任何有效的方法。这篇文章将非常缺乏代码,但我将很快解释我正在尝试做什么。

So I have a listbox that I've successfully populated and it works just fine. At some point as directed by the user, the user will select a row from the listbox, call it RecordBox, Review some information, maybe add some, and then click a "save" command button. Upon Clicking this save button I'd like to copy the selected row from RecordBox to the second listbox. Call it DetailsBox I suppose.

所以我有一个我已经成功填充的列表框,它工作得很好。在用户指示的某个时刻,用户将从列表框中选择一行,将其命名为 RecordBox,查看一些信息,可能添加一些信息,然后单击“保存”命令按钮。单击此保存按钮后,我想将所选行从 RecordBox 复制到第二个列表框。我想称之为DetailsBox。

I either need a way to take data displayed in the form in the form of captions, combobox entries, and text box entries, add a row to "DetailsBox" and copy the information to the particular columns of that row, or I need to simply copy the selected row from RecordBox to DetailsBox.

我要么需要一种方法来获取以标题、组合框条目和文本框条目形式显示在表单中的数据,将一行添加到“DetailsBox”并将信息复制到该行的特定列,或者我需要简单地将所选行从 RecordBox 复制到 DetailsBox。

Anyway, if some of the code would be helpful, just ask, but there really isnt any aside from the command button click event.

无论如何,如果某些代码会有所帮助,请询问,但除了命令按钮单击事件之外,实际上没有任何代码。

I hope that would be enough information.

我希望这是足够的信息。

回答by Siddharth Rout

It's as simple as

就这么简单

ListBox2.AddItem ListBox1.List(ListBox1.ListIndex)

FOLLOWUP (From Comments)

跟进(来自评论)

I think I'm going to send the row to a worksheet and then add it to the other listbox from there.

我想我要将行发送到工作表,然后从那里将其添加到另一个列表框。

I believe you are using a multicolumn listbox. In that case the above code will add only the first column to the 2nd listbox. You need to iterate through the rest of the columns to add the selected row from Listbox1.

我相信您正在使用多列列表框。在这种情况下,上面的代码只会将第一列添加到第二个列表框。您需要遍历其余的列以从Listbox1.

Let's say your userform looks like this. I created a small sample for you.

假设您的用户表单如下所示。我为您创建了一个小样本。

enter image description here

在此处输入图片说明

and the properties of the listboxes are set as below

并且列表框的属性设置如下

enter image description here

在此处输入图片说明

and this is how your Sheet1looks like.

这就是你的Sheet1样子。

enter image description here

在此处输入图片说明

Now put use this code in the 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

When you click select an item in the Listbox1and press the command button, you will notice that the selected row from Listbox1is successfully copied to Listbox2

当您在 中单击选择一个项目Listbox1并按下命令按钮时,您会注意到所选行Listbox1已成功复制到Listbox2

enter image description here

在此处输入图片说明

回答by Blackrock41

For any one looking at sending items from one list box to another using a loop and multi select. Heres some code that might help. Youll need to set both listboxes the properties to Mulitselect. 1-fmMultiSelectMulti. Then use the same settings/setup as Siddharth Rout posted above.

对于任何希望使用循环和多选将项目从一个列表框发送到另一个列表框的人。继承人一些可能有帮助的代码。您需要将两个列表框的属性设置为多选。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