将数组作为参数传递给列表框时出错

时间:2016-08-12 04:19:14

标签: excel vba excel-vba userform

目标:创建用户表单并获取用户输入,然后从用户输入将其放入列表中,当您单击列表时,它会自动在整个工作簿中找到它。

像这样:

enter image description here

我看过这篇文章:Match in whole workbook

我创造了一些东西:

Public Sub CommandButton3_Click()

    Dim TempArray As Variant
    Dim RealArray()

    TempArray = Application.InputBox("Select a range", "Obtain Range Object", Type:=64)
    ArrayRows = UBound(TempArray)
    ReDim RealArray(1 To ArrayRows)
    For i = 1 To ArrayRows
        RealArray(i) = TempArray(i, 1)
    Next i

    MsgBox "The number if rows selected are " & ArrayRows
    ListBox1.List = RealArray
    ListBox1 Arraay:=RealArray

End Sub

Public Sub ListBox1_Click(ByRef Arraay() As Variant)
    Dim Sh As Worksheet
    Dim something As Range
    Dim ArrayRows As Long

    For Each Sh In ThisWorkbook.Worksheets

        With Sh.UsedRange
            For i = 1 To ArrayRows
                Set something = RealArray.Find(What:=RealArray(i))
                If Not something Is Nothing Then
                    Do Until something Is Nothing
                        test = something.Value
                        Set something = .FindNext(something)
                    Loop
                End If
            Next i
        End With
        Set something = Nothing

    Next
End Sub

创建后,我收到有关第二个子的错误。

  

程序声明与具有相同名称的事件或程序的描述不匹配

1 个答案:

答案 0 :(得分:1)

列表框点击事件不带任何参数。

Private Sub ListBox1_Click()

End Sub

如果你想在sub之间传递一个数组,你就可以这样做

Dim MyArray() As Variant

Public Sub CommandButton3_Click()
    '~~> Initialize array
End Sub

Private Sub ListBox1_Click()
    '~~> Use array here
    '~~> Also put an error check if the array is initialized or not
End Sub