【问题标题】:VBA EXCEL adding array members of specific column to collection for counting unique valuesVBA EXCEL将特定列的数组成员添加到集合以计算唯一值
【发布时间】:2017-12-12 11:20:00
【问题描述】:

我需要一个公共函数来获取数组并计算特定列中的值。 我写了以下内容并收到订阅超出范围的消息。

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

【问题讨论】:

  • i + 1 是否会让你在循环期间超出 inarr 的 UBound 范围?
  • 为什么有两个循环?你如何将inarr 传递给你的函数?当您收到错误时,您的代码在哪一行停止?
  • 我将这篇文章修改为: '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'
  • 现在错误为 457 键已与集合元素关联

标签: arrays excel vba count unique


【解决方案1】:

假设您要对数组的指定列中的不同值进行计数,这里是一个从工作表范围读取的 5*3 数组的示例,计算第 2 列中的不同值。我正在使用Mark Nold 的函数来检查集合中是否已经存在要添加的值。

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

【讨论】:

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

    【讨论】:

      猜你喜欢
      • 2019-06-18
      • 1970-01-01
      • 2021-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多