使用excel-vbaselect不同的项目,从列表框中删除项目

我现在很抱歉,因为我是一个绝对的初学者(我的图片和代码还没有被推广)。

我有一个在Excel中的下拉列表框,填充范围我select,而在Excel中(即右键单击ActiveX控件放置后,改变属性)。 我希望这样,如果列表中的某些项目被选中,其他项目将从列表中删除,以便它们不能被选中。 例如。 有一个列表A,B和C,但是当用户selectA时,B从列表中消失。

在这里输入图像说明

我的代码如下。 第一部分代码为下拉列表

Sub Rectangle1_Click() Dim SelShp As Shape, ListShp As Shape, SelList As Variant, i As Integer Set SelShp = Sheet8.Shapes(Application.Caller) Set ListBx = Sheet8.ListBox1 If SelShp.TextFrame2.TextRange.Characters.Text = "Select Buffers" Then ListBx.Visible = True SelShp.TextFrame2.TextRange.Characters.Text = "Set Buffers" Else ListBx.Visible = False SelShp.TextFrame2.TextRange.Characters.Text = "Select Buffers" For i = 0 To Sheet8.ListBox1.ListCount - 1 If Sheet8.ListBox1.Selected(i) = True Then SelList = SelList & "; " & Sheet8.ListBox1.List(i) End If Next i If SelList <> "" Then Range("ListBox1Output") = Right(SelList, Len(SelList) - 1) Else Range("ListBox1Output") = "" End If End If End Sub 

这第二个代码是应该从列表中删除项目

 Private Sub ListBox1_Change() If Sheet8.ListBox1.Selected(0) Then Sheet8.ListBox1.RemoveItem 1 End If End Sub 

问题是,当我尝试出来,我得到一个运行时错误“-2147467259(80004005)”:未指定的错误,如果我尝试debugging它突出显示'Sheet8.ListBox1.RemoveItem 1',但我只是不我不知道自己做错了什么。 任何帮助将不胜感激,如果我错过了一些非常简单的事情,我很抱歉。


编辑:我一直在这个工作,因为我张贴,并已find一些解决scheme,但遇到其他路障。

我的第一个问题是.RemoveItem方法没有做任何事情。 如果使用.ListFillRange方法填充ListBox,那么.RemoveItem将不起作用 – 如果稍后要使用.RemoveItem,则必须使用.AddItem填充ListBox。

在我解决这个问题之后,我决定尝试用更简单的数据来做我想做的事情:

我有2个列表框,我用数据填充其中的一个。 在ListBox1中select一个项目后,该项目被复制到ListBox2中,并从ListBox1中删除。 另外,如果select了ListBox1中的某些项目,则其他项目将从列表框中删除,以便它们不能被选中。 例如。 有一个列表A,B和C,但是当用户selectA时,B从列表中消失。

我已经得到了我的代码,以在某些情况下工作的地步。 不幸的是,这些项目的顺序是重要的,由于某种原因,对于某些项目序列,代码不能按预期工作 – 例如,我的概括项目恰好是:德国,印度,法国,美国,英国。 当select“德国”时,该项目出现在列表框2中,它从列表框1中移除,并且从列表框1中移除“法国”。 这工作正常,直到项目按字母顺序,在这一点上select“德国”,这个项目出现在列表框2中,它从列表框1中删除,“法国”从列表框1中删除,AND印度和美国移动到列表框2 …? 就好像一旦“法国”被删除了,下面的任何东西都被选中,并且由于某种原因,通过ListBox1_Change()的前两个循环来运行。 用一个消息框中断它的工作原因,但我不知道如何中断它,而不使用消息框…

我的代码如下,有一些评论我所包含的内容。

用随机位置的项目填充ListBox1

 Sub Populate_ListBox1() 'Clear LB1 before populating it Sheet1.ListBox1.Clear Sheet1.ListBox2.Clear Sheet1.ListBox1.AddItem "Germany" Sheet1.ListBox1.AddItem "India" Sheet1.ListBox1.AddItem "France" Sheet1.ListBox1.AddItem "USA" Sheet1.ListBox1.AddItem "England" End Sub 

尝试移动选定的ListBox1项目,同时更改ListBox1中的项目

 Private Sub ListBox1_Change() 'Variable Declaration Dim iCnt As Integer Dim jCnt As Integer Dim kCnt As Integer 'Move Selected Item from Listbox1 to Listbox2 For iCnt = 0 To Me.ListBox1.ListCount - 1 If Me.ListBox1.Selected(iCnt) = True Then Me.ListBox2.AddItem Me.ListBox1.List(iCnt) End If Next 'Clear Selected Item from Listbox1 For iCnt = Me.ListBox1.ListCount - 1 To 0 Step -1 If Me.ListBox1.Selected(iCnt) = True Then Me.ListBox1.RemoveItem iCnt 'Me.ListBox1.Selected(iCnt) = False 'Nope 'Exit For End If Next 'If Germany is in Listbox2, then remove France from LB1 For kCnt = 0 To Me.ListBox2.ListCount - 1 If Me.ListBox2.Column(0, kCnt) = "Germany" Then For jCnt = 0 To Me.ListBox1.ListCount - 1 If Me.ListBox1.Column(0, jCnt) = "France" Then Me.ListBox1.RemoveItem jCnt 'Me.ListBox1.Locked = True 'Nope 'Me.ListBox1.Enabled = False 'Nope 'Me.ListBox1.ListIndex = -1 'This crashes excel... 'MsgBox "blah" 'For some reason this works >.< Exit Sub End If Next jCnt End If Next End Sub 

我真的很感谢这方面的帮助,甚至会提出build议,使用一个不同的程序,可以与Excel一起工作(试图改变项目在列表框中的索引,这种变化 ,而不是他们的值是一场噩梦)