2016-08-12 63 views
1

目標:創建一個用戶表單並獲取用戶輸入,然後從用戶輸入將其放入列表中,當您單擊該列表時,它會自動在整個工作簿中找到它。將數組作爲參數傳遞給列表框時的錯誤

是這樣的:

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 

創建此之後,我得到一個錯誤關於第二個子。

過程聲明不匹配事件或過程具有相同名稱

+0

'公用Sub ListBox1_Click(爲ByRef Arraay()爲Variant)'這不帶任何參數。它應該是'ListBox1_Click()' –

+0

@SiddharthRout但是我怎麼傳遞子之間的數組?這不是那種方法嗎?對不起,我對VBA很陌生 –

回答

1

列表框中單擊事件不帶任何參數的描述。

Private Sub ListBox1_Click() 

End 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 
相關問題