如何在公式中引用集合

时间:2019-05-12 10:31:18

标签: excel vba

我要做的就是在公式中引用一个集合。喜欢

假定我已经知道如何制作集合和数组,并且已经在我的宏中做到了,集合实际上是一个只有1列的集合,而Textstring是一个数组向量。

'For every value of i in Textstring, I want to count the occurrence of that value in all the values of 'Collection'
For i = 1 to Whatever 
    =COUNTIF(Collection, """ & TextString(i) & """)
Next i

我想知道的是如何使上述代码起作用。

它应该像普通计数器一样工作:

'ie: "=COUNTIF('Sheet1'!A1:A10, ""blah"")"

1 个答案:

答案 0 :(得分:1)

您不能使用COUNTIF,如果您看一下该函数的参数,它将需要一个 Range 对象。

enter image description here

我唯一的建议是执行以下操作,即将其写到工作表中,然后将该范围用作函数的参数...

Public Sub CollectionToRange()
    Dim objCollection As New Collection, i As Long

    For i = 1 To 10
        objCollection.Add i
    Next

    ' Convert the collection to a range.
    For i = 1 To objCollection.Count
        Sheet1.Cells(i, 1) = objCollection.Item(i)
    Next

    ' Pass the range into the worksheet function.
    Debug.Print "CountIf Result = " & WorksheetFunction.CountIf(Sheet1.Range("A1:A" & objCollection.Count), ">3")

    ' Perform a clean up if required.
End Sub

enter image description here

不确定是否有帮助。