2017-12-12 15 views
0

特定の列に配列とカウント値を取得するには、public関数が必要です。 私は以下を書いて、サブスクリプション・アウト・オブ・レンジ・メッセージを受け取りました。あなたは、配列の指定された列内の個別値のカウントをしたいと仮定すると、VBA EXCEL特定の列の配列メンバーをコレクションに追加して一意の値をカウントする

Public Function CountUarrcol(inarr() As Variant, colidx As Integer) As Long 
Dim col As New Collection 
Dim i As Integer 
Dim element As Variant 

For i = 0 To UBound(inarr, colidx) 
    For Each element In inarr(i + 1, colidx) 
     col.Add Item:=CStr(element.value), Key:=CStr(element.value) 
    Next 
Next i 
CountUarrcol = col.Count End Function 
+0

はI + 1は、UBound関数を超えてあなたを入れていますループ中のinarrの? – QHarr

+0

なぜ2つのループがありますか?どのようにあなたの関数に 'inarr'を渡していますか?エラーが発生したときにコードが停止している行は何ですか? –

+0

私はこの部分を次のように改訂しました:'Public Function CountUarrcol(inarr As Variant, colidx As Integer) As Long Dim col As New Collection Dim i As Integer Dim element As Variant For Each element In inarr col.Add Item:=element, Key:=element Next CountUarrcol = col.Count End Function' hussein5

答えて

0

は、ここでは2列に個別の値を数え、ワークシートの範囲から読み込ま5 * 3の配列を持つ例です。 Mark Noldによって関数を使用して、追加する値がコレクションに既に存在するかどうかを確認しています。 OKだった

Option Explicit 

Public Sub test() 

    Dim testArr() 
    Dim myCount As Long 

    testArr = ActiveSheet.Range("A1:C5").Value 

    myCount = CountUarrcol(testArr, 2) 

    MsgBox myCount 

End Sub 

Public Function CountUarrcol(inarr() As Variant, colidx As Long) As Long 

    Dim col As New Collection 
    Dim i As Long 

    For i = 1 To UBound(inarr) 

     If Not InCollection(col, CStr(inarr(i, colidx))) Then 

      col.Add Item:=CStr(inarr(i, colidx)), key:=CStr(inarr(i, colidx)) 

     End If 

    Next i 

    CountUarrcol = col.Count 

End Function 

'Mark Nold https://stackoverflow.com/questions/137845/determining-whether-an-object-is-a-member-of-a-collection-in-vba 

Public Function InCollection(col As Collection, key As String) As Boolean 
    Dim var As Variant 
    Dim errNumber As Long 

    InCollection = False 
    Set var = Nothing 

    Err.Clear 
    On Error Resume Next 
    var = col.Item(key) 
    errNumber = CLng(Err.Number) 
    On Error GoTo 0 

    '5 is not in, 0 and 438 represent incollection 
    If errNumber = 5 Then      ' it is 5 if not in collection 
     InCollection = False 
    Else 
     InCollection = True 
    End If 

End Function 

Running macro

0

。我々はそれを行うための2つのサブが必要になります。

Public Function CountUvalinarrcol(ByRef inarr As Variant, ByVal colidx As Integer) As Long 
    Dim col As New Collection 
    Dim i As Integer 
    Dim element As Variant 

    For i = 1 To UBound(inarr) 
    element = inarr(i, colidx) 

     If colContains(col, element) = False Then 
      col.Add item:=CStr(element) 
     End If 
    Next i 
    CountUvalinarrcol = col.Count 
End Function 

他方は次のとおりです。

Public Function colContains(colin As Collection, itemin As Variant) As Boolean 
Dim item As Variant 
    colContains = False 
    For Each item In colin 
    If item = itemin Then 
     colContains = True 
     Exit Function 
    End If 
    Next 
End Function 

上記の関数を呼び出す:

sub test() 
dim x as long 
x= CountUvalinarrcol(lsarr, 0) 
end sub 
関連する問題