2016-10-02 22 views
0

セルの範囲から関数内で定義された配列から特定の数値を数えたいと思います。ここに私のコードです:Vba Excelタイプの不一致

Function countNumbers(cell As Range) 

    Dim rCell As Range 
    Dim myArray(25) As Integer 
    myArray(0) = 1 
    myArray(1) = 2 
    myArray(2) = 3 
    myArray(3) = 4 
    myArray(4) = 5 
    myArray(5) = 11 
    myArray(6) = 12 
    myArray(7) = 13 
    myArray(8) = 14 
    myArray(9) = 15 
    myArray(10) = 21 
    myArray(11) = 22 
    myArray(12) = 23 
    myArray(13) = 24 
    myArray(14) = 25 
    myArray(15) = 31 
    myArray(16) = 32 
    myArray(17) = 33 
    myArray(18) = 34 
    myArray(19) = 35 
    myArray(20) = 41 
    myArray(21) = 42 
    myArray(22) = 43 
    myArray(23) = 44 
    myArray(24) = 45 

    For Each rCell In cell.Cells 
     For i = LBound(myArray) To UBound(myArray) 
      If rCell.Value = myArray Then 
       countNumbers = countNumbers + 1 
      End If 
     Next i 
    Next rCell 

End Function 

私が得ているエラーは、コードの最初の行に "タイプの不一致"です。あなたがチェックしようとしているとして、私はあなたのループ内のMS-エクセル2007のおかげ

+2

を使用しています、rCell.Value = MyArrayというThen'もしIfはrCell.Value = myarrayの(I)Then' 'へあなたの行を'修正配列内の各セルの基準 –

+1

また、 'Dim myArray(25)As Integer'を' Dim myArray(24)As Integer'に変更してください。さもなければ最後の要素は空になり、空のセルは ' if'基準。 –

+0

@ShaiRadoあなたはそれを受け入れる答えとしてあなたの訂正を追加できますか? – KostasC

答えて

1
Function countNumbers(cell As Range) 

    Dim rCell As Range 
    Dim myArray(24) As Integer 

    myArray(0) = 1 
    myArray(1) = 2 
    myArray(2) = 3 
    myArray(3) = 4 
    myArray(4) = 5 
    myArray(5) = 11 
    myArray(6) = 12 
    myArray(7) = 13 
    myArray(8) = 14 
    myArray(9) = 15 
    myArray(10) = 21 
    myArray(11) = 22 
    myArray(12) = 23 
    myArray(13) = 24 
    myArray(14) = 25 
    myArray(15) = 31 
    myArray(16) = 32 
    myArray(17) = 33 
    myArray(18) = 34 
    myArray(19) = 35 
    myArray(20) = 41 
    myArray(21) = 42 
    myArray(22) = 43 
    myArray(23) = 44 
    myArray(24) = 45 

    For Each rCell In cell.Cells 
     For i = LBound(myArray) To UBound(myArray) 
      If rCell.Value = myArray(i) Then 
       countNumbers = countNumbers + 1 
      End If 
     Next i 
    Next rCell 

End Function