【发布时间】: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