Hello everyone!
Suppose I have the following range:
[TABLE="class: grid, width: 250"]
<tbody>[TR]
[TD]Project Name[/TD]
[TD]Color[/TD]
[/TR]
[TR]
[TD]Project A[/TD]
[TD]yellow[/TD]
[/TR]
[TR]
[TD]Project A[/TD]
[TD]green[/TD]
[/TR]
[TR]
[TD]Project A[/TD]
[TD]red[/TD]
[/TR]
[TR]
[TD]Project A[/TD]
[TD]green[/TD]
[/TR]
[TR]
[TD]Project A[/TD]
[TD]green[/TD]
[/TR]
[TR]
[TD]Project B[/TD]
[TD]blue[/TD]
[/TR]
[TR]
[TD]Project B[/TD]
[TD]yellow[/TD]
[/TR]
[TR]
[TD]Project B[/TD]
[TD]yellow[/TD]
[/TR]
[TR]
[TD]Project B[/TD]
[TD]blue[/TD]
[/TR]
</tbody>[/TABLE]
I would like to code a custom function that check the first column according to a given criteria and count the number of different colors in the second column. For example, the number of different colors of "Project A" is 3 (yellow, green and red).
I managed to do the folowing function:
The function is working, but it gets so heavy to calculate when I select a entire range, example: =COUNT_DATA(A6;A:A;B:B)
The built-in worksheets functions, like COUNTIF or VLOOKUP doesn't take any time for calculating when I select entire ranges. Why is this happening? What I'm doing wrong?
Is there a way to view the built-in functions in VBA language, just to learn a little bit more?
Thanks!
Suppose I have the following range:
[TABLE="class: grid, width: 250"]
<tbody>[TR]
[TD]Project Name[/TD]
[TD]Color[/TD]
[/TR]
[TR]
[TD]Project A[/TD]
[TD]yellow[/TD]
[/TR]
[TR]
[TD]Project A[/TD]
[TD]green[/TD]
[/TR]
[TR]
[TD]Project A[/TD]
[TD]red[/TD]
[/TR]
[TR]
[TD]Project A[/TD]
[TD]green[/TD]
[/TR]
[TR]
[TD]Project A[/TD]
[TD]green[/TD]
[/TR]
[TR]
[TD]Project B[/TD]
[TD]blue[/TD]
[/TR]
[TR]
[TD]Project B[/TD]
[TD]yellow[/TD]
[/TR]
[TR]
[TD]Project B[/TD]
[TD]yellow[/TD]
[/TR]
[TR]
[TD]Project B[/TD]
[TD]blue[/TD]
[/TR]
</tbody>[/TABLE]
I would like to code a custom function that check the first column according to a given criteria and count the number of different colors in the second column. For example, the number of different colors of "Project A" is 3 (yellow, green and red).
I managed to do the folowing function:
Function COUNT_DATA(criteria As String, CritRange As Variant, DataRange As Variant)
Dim i As Long
Dim MyArray() As Variant
Dim N As Long
Dim verify As Variant
N = 0
ReDim MyArray(N)
For i = LBound(CritRange.Value) To UBound(CritRange.Value)
If CritRange(i, 1) = criteria Then
verify = Application.match(DataRange(i, 1), MyArray, 0)
If IsError(verify) Then
ReDim Preserve MyArray(N)
MyArray(N) = DataRange(i, 1)
N = N + 1
End If
End If
Next i
COUNT_DATA = N
End Function
The function is working, but it gets so heavy to calculate when I select a entire range, example: =COUNT_DATA(A6;A:A;B:B)
The built-in worksheets functions, like COUNTIF or VLOOKUP doesn't take any time for calculating when I select entire ranges. Why is this happening? What I'm doing wrong?
Is there a way to view the built-in functions in VBA language, just to learn a little bit more?
Thanks!