Count Unique Numerical Values that begin with

pauln93

New Member
Joined
Aug 2, 2018
Messages
3
Hi,

I'm making a database right now that need a formula for counting unique numerical values that begin with a certain number, because of the amount of duplicates. All the formula I tried doing myself have resulted in errors :(
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Welcome to Mr Excel forum

Could you provide a small data sample (~10 rows) along with criteria and expected result?

M.
 
Upvote 0
Hi,

This is the set of 10, and I'm looking to say count things that begin with 8 but doesn't include duplicates

8404
8404
8404
8449
8450
8522
8553
8553
8553
8558
8562

<colgroup><col></colgroup><tbody>
</tbody>
 
Upvote 0
How about this?

Code:
=SUMPRODUCT(((LEFT(NRange,1)+0)=8)/COUNTIF(NRange,NRange&""))

Where nRange is your range of data.

Also, this is an array formaula so hit control + shift + enter agter pasting the formula in.
 
Upvote 0
Having said that, I think a better solution would be to use a helper column to extract the first digit of each number. So, if your data is in column A, then column B would have the formula = Left(A2,1). Then just put the whole thing into a Pivot Table. A lot more dynamic and useful IMO.
 
Last edited:
Upvote 0
Try

For testing purposes I included two rows with numbers that don't begin with 8

A
B
C
D
1
Numbers​
Begin with​
Count Unique​
2
8404​
8​
7​
3
8404​
4
8404​
5
8449​
6
8450​
7
8522​
8
8553​
9
8553​
10
8553​
11
8558​
12
8562​
13
9404​
14
9404​
15

Criteria in C2

Array formula in D2
=SUM(IF(FREQUENCY(IF(0+LEFT(A2:A14)=C2,A2:A14),A2:A14),1))
Ctrl+Shift+Enter, not just Enter

Hope this helps

M.
 
Upvote 0
For those who might find it interesting, here is a UDF (user defined function) solution (which can be called from within other VB code if needed)...
Code:
[table="width: 500"]
[tr]
	[td]Function UniqueCount(Rng As Range, BeginWith As String) As Long
  Dim V As Variant, Data As Variant
  Data = Rng.Value
  With CreateObject("Scripting.Dictionary")
    For Each V In Data
      If Left(V, Len(BeginWith)) = BeginWith Then .Item(V) = 1
    Next
    UniqueCount = .Count
  End With
End Function[/td]
[/tr]
[/table]
This function takes two arguments... first is the range to look at and the second is the beginning text (one or more characters) to search for.

HOW TO INSTALL UDFs
------------------------------------
If you are new to UDFs, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. You can now use UniqueCount just like it was a built-in Excel function. For example,

=UniqueCount(A1:A10,"8")

If you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
 
Upvote 0
You are welcome.

The 0+ converts the value returned by LEFT (a string) to a number - it's necessary because C2 (criteria) contains a number.

M.
 
Upvote 0
another way is PivotTable with DataModel to count distinct values and Label filter Begins with... 8
 
Upvote 0

Forum statistics

Threads
1,215,851
Messages
6,127,302
Members
449,374
Latest member
analystvar

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