Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 对象所需的运行时错误424_Excel_Vba - Fatal编程技术网

Excel 对象所需的运行时错误424

Excel 对象所需的运行时错误424,excel,vba,Excel,Vba,我有一个复选框列表,在选中其中一些复选框后,我想知道选中了哪些复选框,以便我可以使用这些复选框。不知道为什么这几行行不通。执行该命令后,会弹出一条错误消息,显示“Object required”运行时错误“424”:并突出显示line=>ReDim SelectEditeArray(ListBox1.ListCount)作为字符串。是的,我有四个列表框;列表框1、列表框2、列表框3、列表框4。感谢您的帮助。多谢各位 Sub CheckedBoxes() Dim SelectedItemArra

我有一个复选框列表,在选中其中一些复选框后,我想知道选中了哪些复选框,以便我可以使用这些复选框。不知道为什么这几行行不通。执行该命令后,会弹出一条错误消息,显示“Object required”运行时错误“424”:并突出显示line=>ReDim SelectEditeArray(ListBox1.ListCount)作为字符串。是的,我有四个列表框;列表框1、列表框2、列表框3、列表框4。感谢您的帮助。多谢各位

Sub CheckedBoxes()

Dim SelectedItemArray() As String

ReDim SelectedItemArray(ListBox1.ListCount) As String

For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) = True Then
SelectedItemArray(i) = ListBox1.List(i)
End If
Next

End Sub

您需要完全限定列表框。例如
Sheet1.ListBox1.ListCount

这是一个用于用户表单上列表框的函数。我对其进行了修改(如下),以便在工作表列表框中使用

对于UserForm上的表单控件ListBox,可以这样调用它:

myArray = GetSelectedItems(ListBox1)
下面是一个函数,它将接受来自UserForm的任何listbox作为命名参数:

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
。Listbox在工作表或用户表单上?谢谢,谢谢,谢谢,非常感谢Dave。非常感谢你。谢谢你解决了我的问题。非常感谢大卫。我非常感谢你的帮助。