excel vba listbox变量类型未命中匹配

时间:2016-01-13 14:56:19

标签: excel vba listbox

我现在一直在努力将列表框传递给子。经过3天的搜索......我需要帮助。我的最终目标是在一个列表框中选择项目,然后单击按钮并将其移动到另一个列表框。这是我到目前为止所做的尝试。

在UserForm中:

Private Sub CommandButton7_Click()
Dim lbINDX As Integer
Dim lbSource As String, lbDest As String

lbINDX = ListBox3.ListIndex
lbSource = Me.ListBox3.Name
lbDest = Me.ListBox1.Name
Call move(lbINDX, lbSource, lbDest)

End Sub

在Userform sub中我也试过:(没有.name)lbSource& lbDest作为object,control,listbox和msforms.listbox

在模块中:

Private Sub move(itm As Integer, objTarget As String, objDest As String)

txt = objTarget.ListIndex(itm).Text
UserForm10.Controls(objTarget).RemoveItem Item:=itm
UserForm10.Controls(objDest).AddItem txt

End Sub

在模块子I中我也试过:(有和没有Userform10.Controls())objTarget& objDest作为object,control,listbox和msforms.listbox

如何通过变量将列表框作为一个整体传递?或者有更简单的方法吗?

针对Nathan_Sav的修订版1:

Private Sub CommandButton7_Click()
Dim lbINDX As Long
Dim lbSource As MSForms.ListBox, lbDest As MSForms.ListBox

lbINDX = ListBox3.ListIndex
Set lbSource = Me.ListBox3
Set lbDest = Me.ListBox1
Call move(lbINDX, lbSource, lbDest)

End Sub

Private Sub move(lngIndex As Long, objSource As MSForms.ListBox, objDest As MSForms.ListBox)

objDest.AddItem objSource.ListIndex(lngIndex).Text
objSource.RemoveItem Item:=lngIndex

End Sub

2 个答案:

答案 0 :(得分:0)

我会这样做,没有经过测试

Public Sub MoveEntry(cboSource As MSForms.ComboBox, cboDestination As MSForms.ComboBox, lngIndex As Long)
    cboDestination.AddItem cboSource.ListIndex(lngIndex).Text
    cboSource.RemoveItem Index:=lngIndex
End Sub

答案 1 :(得分:0)

在Nathan_Sav的一些帮助和一些进一步的修补之后,我得到了以下内容。虽然它做了我需要的东西,但它并没有完全完成,因为它将值添加到目标列表框的底部,无论有多少空白(“”或null或其他)值。

在Userform中

Private Sub CommandButton7_Click()
Dim lbINDX As Long
Dim lbSource As MSForms.ListBox, lbDest As MSForms.ListBox

lbINDX = ListBox3.ListIndex
Set lbSource = Me.ListBox3
Set lbDest = Me.ListBox1
Call Module1.MoveEntry(lbINDX, lbSource, lbDest)

End Sub

在模块中     Sub MoveEntry(lngIndex As Long,objSource As MSForms.ListBox,objDest As MSForms.ListBox) Dim txt As String

MsgBox objSource.Name
txt = objSource.Value
objDest.AddItem txt
objSource.RemoveItem lngIndex

End Sub

目前我只允许传输一个值。我怀疑如果列表框是多选的,则需要进一步修改。

非常感谢Nathan的帮助。

如果有人可以解释为什么这有效,但其他尝试没有请发表答案,以便我可以回答。