VBA WorksheetFunction to Count the number of unique values in a range

longbow2000

Board Regular
Joined
May 5, 2004
Messages
59
Office Version
  1. 365
Platform
  1. Windows
Good Day

As part of a formula in a cell to count the unique values in a range (“A2:A10”), the following is used:

=SUM(IF(A2:A10<>"",1/COUNTIF(A2:A10, A2:A10), 0))



I am looking for a similar VBA worksheet function (Application.WorksheetFunction.CountIf????), but instead of referencing the rage as A2:A10, it must be cell address with variables.

As an example (myCell(x),myCol(x)): (myCell(z),myCol(z))

Based on the data layout, I can then run a loop function

Thanks in advance

Mr_Excel.xlsm
ABC
1LinkID_SectionID
2D1088_010# Unique Val
3D1088_0204
4D1088_030
5D1088_030
6D1088_030
7D1088_030
8D1088_030
9D1088_030
10D1088_040
11D1083_0102
12D1083_010
13D1083_010
14D1083_010
15D1083_010
16D1083_020
WorksheetFunction
Cell Formulas
RangeFormula
C3C3=SUM(IF(A2:A10<>"",1/COUNTIF(A2:A10, A2:A10), 0))
C11C11=SUM(IF(A11:A16<>"",1/COUNTIF(A11:A16, A11:A16), 0))
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Do you need VBA, you can do that with a formula
Fluff.xlsm
ABC
1LinkID_SectionID# Unique Val
2D1088_0104
3D1088_020 
4D1088_030 
5D1088_030 
6D1088_030 
7D1088_030 
8D1088_030 
9D1088_030 
10D1088_040 
11D1083_0102
12D1083_010 
13D1083_010 
14D1083_010 
15D1083_010 
16D1083_020 
17 
18 
19 
Master
Cell Formulas
RangeFormula
C2:C19C2=IF(OR(A2="",LEFT(A2,5)=LEFT(A1,5)),"",ROWS(UNIQUE(FILTER(A2:A100,LEFT(A2:A100,5)=LEFT(A2,5)))))
 
Upvote 0
Hi Fluff

Yes, I had it as formula in a cell, but the range size and variable change daily.

Since my post, I have it now as part of VBA in a loop and the module runs correct, as follows:

VBA Code:
Dim myRngE() As Integer, myRngS() As Integer, myUniqueR() As Integer

‘Values for myRngE() and myRngS() obtained earlier in module

‘nrRanges and mySecRng also obtained earlier



'For # Unique Range

ReDim myUniqueR(1 To nrRanges)

For z = 1 To nrRanges

'For # Unique Range

mySecRng = myRngE(z) - myRngS(z) + 1

i = myRngS(z)

For i = myRngS(z) To myRngE(z)

nxtr = i + 1

If ws.Cells(i, 1).Value = ws.Cells(nxtr, 1) Then

mySecRng = mySecRng - 1

End If

Next i

myUniqueR(z) = mySecRng

wsTemp.Cells(z + 1, 7) = myUniqueR(z)

Next z

Thanks
Ivan
 
Upvote 0
Solution
Glad you sorted it & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,262
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top