Excel多选,多列列表框 [英] Excel multi-select, multi-column listboxes

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

问题描述

我在编写一个用户表单时遇到麻烦,该用户表单从一个多列列表框中获取所选数据,然后将其添加到来自同一用户的另一个列表框中.添加后,数据将从源列表框中删除.

I am having trouble coding a userform that takes the selected data from one multi-column Listbox and adds it to another Listbox on the same userfrom. after adding, the data is removed from the source Listbox

"ListBox"是数据所在的位置,"listbox1"是要添加数据的位置.

"ListBox" is where the data is located, and "listbox1" is where it is being added to.

Private Sub add_Click()

For i = 0 To ListBox.ListCount - 1
    If ListBox.Selected(i) = True Then
        NextEmpty = ListBox1.ListCount
        ListBox1.List(NextEmpty, 0) = ListBox.List(i, 0)
        ListBox1.List(NextEmpty, 1) = ListBox.List(i, 1)
        ListBox1.List(NextEmpty, 2) = ListBox.List(i, 2)
        ListBox.RemoveItem (i)
    End If
Next

End Sub

这段代码给我一个运行时错误'381' 无法设置列表属性.无效的属性数组索引." 我已经做了一些环顾四周,但似乎无法查明如何正确使用这些属性.任何帮助是极大的赞赏.

This code gives me a Run-time error '381' "Could not set the list property. Invalid property array index." I have done some looking around but can't seem to pinpoint how to use these properties correctly. Any help is greatly appreciated.

推荐答案

为此,就像Daniel所说的那样,我们需要使用add函数. 在下面的代码中,您可以看到我如何在我的with-block中使用.additem函数.
要在将所选内容移至新的列表框后将其删除,请运行向后循环.
For i = MainListBox.ListCount - 1 To 0 Step -1

In order to do this, like Daniel said, we need to use an add function. In the code below you can see how I used the .additem function in my with-block.
To remove the selection after moving it to a new Listbox, I run a backwards loop.
For i = MainListBox.ListCount - 1 To 0 Step -1

Private Sub add_Click()

Dim i As Integer
For i = 0 To MainListBox.ListCount - 1
    If MainListBox.Selected(i) Then
            With ListBox1
            .AddItem
            .List(.ListCount - 1, 0) = MainListBox.List(i, 0)
            .List(.ListCount - 1, 1) = MainListBox.List(i, 1)
            .List(.ListCount - 1, 2) = MainListBox.List(i, 2)
            End With
        End If
    Next i

For i = MainListBox.ListCount - 1 To 0 Step -1
    If MainListBox.Selected(i) Then
        MainListBox.RemoveItem (i)
    End If
Next i

End Sub

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

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