0

I was wondering if there is a possibility to use countif on arrays.

Currently there are two arrays. One is the Array with the Range (RangeArray) and the other the Criteria array (CritArray) which comes from another workbookbut is saved in an array. I'm trying to use the countif method in VBA using arrays if and store the countif values in a cell. So I don't need to loop between workbooks all the time.

Dim RangeArray, CritArray as Variant

RangeArray = Array(1,2,3,4,2,4,2,5,7,1,7,1,2)
CritArray = Array(1,2)

For i = 1 To LastRow
    Cells(i, 1).Value = WorksheetFunction.CountIf(RangeArray, CriteriaArray)
Next i

When I try to do something amongst these lines it keeps giving the error "object required".

Any help would be kindly appreciated!

Kind regards,

2
  • 2
    Arg1 of CountIf must be a Range. See the docs. Commented Mar 6, 2020 at 14:07
  • 1
    What should the result be given the two arrays in your question? Commented Mar 6, 2020 at 14:18

1 Answer 1

1
Sub test()
Dim RangeArray, CritArray As Variant
Dim Counts As New Collection

RangeArray = Array(1, 2, 3, 4, 2, 4, 2, 5, 7, 1, 7, 1, 2, 11)
CritArray = Array(1, 2)

For i = 0 To UBound(CritArray)
Count = 0
    For j = 0 To UBound(RangeArray)
    If CritArray(i) = RangeArray(j) Then
    Count = Count + 1
    End If
    Next
Counts.Add Count
Next

For k = 1 To Counts.Count
Cells(k, 1) = Counts(k)
Next
End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.