VBA列表框复制到列表框 [英] VBA listbox copy to listbox

查看:285
本文介绍了VBA列表框复制到列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这似乎坚果,我一直在研究它几个小时,但我找不到任何有效的。这个帖子将是非常缺乏代码,但我会很快解释我想要做什么。



所以我有一个列表框我已成功填充,它的工作很好。在用户指示的某个时间点,用户将从列表框中选择一行,调用它RecordBox,查看一些信息,也可以添加一些,然后点击保存命令按钮。点击此保存按钮后,我想将所选的行从RecordBox复制到第二个列表框。调用它DetailsBox我想。



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



无论如何,如果一些代码是有帮助的,只是问,但真的除了命令按钮单击事件除外。



我希望这是足够的信息。

解决方案

它很简单

  ListBox2.AddItem ListBox1.List(ListBox1.ListIndex)

FOLLOWUP )


我想将该行发送到工作表,然后将其添加到其他列表框。


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



看起来像这样。我为您创建了一个小样本。







//i.stack.imgur.com/omKBt.pngalt =enter image description here>



这是您的 Sheet1 看起来像。





现在在Userform中使用这个代码。

  Private Sub UserForm_Initialize()
'~~>将示例数据添加到listbox 1
ListBox1.List = ThisWorkbook.Sheets(1).Range(A1:E3)。
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

对于k = 1至.ColumnCount - 1
ListBox2.List k)= .List(i,k)
下一个k
结束于
结束子

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




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.

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.

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.

解决方案

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.

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

and the properties of the listboxes are set as below

and this is how your Sheet1 looks like.

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 Listbox1 and press the command button, you will notice that the selected row from Listbox1 is successfully copied to Listbox2

这篇关于VBA列表框复制到列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆