這是我用於UserForm上的ListBoxes的函數。我修改了它(進一步在下面)以供在Worksheet列表框中使用。
對於表單控件在用戶窗體列表框,把它想:
myArray = GetSelectedItems(ListBox1)
這裏將從用戶窗體接受任何列表框爲命名參數的函數:
Public Function GetSelectedItems(lBox As MSForms.ListBox) As Variant
'returns an array of selected items in a ListBox
Dim tmpArray() As Variant
Dim i As Integer
Dim selCount As Integer
selCount = -1
For i = 0 To lBox.ListCount - 1
If lBox.Selected(i) = True Then
selCount = selCount + 1
ReDim Preserve tmpArray(selCount)
tmpArray(selCount) = lBox.List(i)
End If
Next
If selCount = -1 Then
GetSelectedItems = Array()
Else:
GetSelectedItems = tmpArray
End If
End Function
如果您是指工作表上的列表框,請嘗試使用此代替:
將其命名爲:
myArray = GetSelectedItems(Sheet1.Shapes("List Box 1").OLEFormat.Object)
這裏的修改工作表表單控件列表框的功能:
Public Function GetSelectedItems(lBox As Object) As Variant
'returns an array of selected items in a ListBox
Dim tmpArray() As Variant
Dim i As Integer
Dim selCount As Integer
selCount = -1
For i = 1 To lBox.ListCount - 1
If lBox.Selected(i) = True Then
selCount = selCount + 1
ReDim Preserve tmpArray(selCount)
tmpArray(selCount) = lBox.List(i)
End If
Next
If selCount = -1 Then
GetSelectedItems = Array()
Else:
GetSelectedItems = tmpArray
End If
End Function
嘗試完全限定列表框。例如'Sheet1.ListBox1.ListCount'。 –
列表框位於工作表或用戶表單上嗎? –
謝謝,謝謝,謝謝,非常感謝Dave。非常感謝你。謝謝你解決了我的問題。 –