【问题标题】:Error passing array to listbox as a parameter将数组作为参数传递给列表框时出错
【发布时间】:2017-03-21 20:29:03
【问题描述】:

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

类似这样的:

我看到了这个帖子: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

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

过程声明与同名事件或过程的描述不匹配

【问题讨论】:

  • Public Sub ListBox1_Click(ByRef Arraay() As Variant) 不带任何参数。应该是ListBox1_Click()
  • @SiddharthRout 但是如何在 sub 之间传递数组?这不是方法之一吗?抱歉,我对 VBA 很陌生

标签: vba excel userform


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-08
    • 2013-01-24
    • 1970-01-01
    • 1970-01-01
    • 2014-12-17
    • 1970-01-01
    • 2012-03-07
    • 2019-07-23
    相关资源
    最近更新 更多