我一直在努力一段時間,將列表框傳遞給子。經過3天的搜索......我需要幫助。我的最終目標是在一個列表框中選擇項目,然後單擊按鈕並將它移動到另一個列表框。這是迄今爲止我嘗試過的。excel vba listbox變量類型錯過匹配
在用戶窗體:
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
在用戶窗體子我也嘗試:(無。名稱)lbSource & lbDest作爲對象,控制,列表框,和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
在模塊的子我也嘗試:(有和沒有Userform10.Controls())objTarget & objDest作爲對象,控制,數組listB ox和msforms.listbox
如何通過變量傳遞整個列表框?還是有更簡單的方法?
修訂1響應Nathan_Sav:
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
Appologies,我忽略了一個關鍵部分。我在「呼叫移動(lbINDX,lbSource,lbDest) – gde211
第一個變種,itm需要很長時出現」類型不匹配「錯誤https://msdn.microsoft.com/en-us/library/office/ff193579.aspx它也需要像索引:= itm –
@Nathan_Sav得到'長'部分,雖然我不知道爲什麼,但'像索引:= itm'扔我離開。這將去哪裏?在模塊或用戶表單子? – gde211