gpt4 book ai didi

VBA 列表框复制到列表框

转载 作者:行者123 更新时间:2023-12-04 04:39:52 25 4
gpt4 key购买 nike

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

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

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

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

我希望这是足够的信息。

最佳答案

就这么简单

ListBox2.AddItem ListBox1.List(ListBox1.ListIndex)

跟进(来自评论)

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



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

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

enter image description here

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

enter image description here

这就是您的 Sheet1看起来像。

enter image description here

现在把这个代码放在用户表单中。
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
enter image description here

关于VBA 列表框复制到列表框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19064043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com