Count Unique Values in a Cell - and the Entire Range that Cell Resides In

Philometis

New Member
Joined
May 19, 2014
Messages
32
I have a worksheet that has a column of cells but each cell has one to several field names. I find lots of guidance on counting unique values in a range of cells BUT not within cell and ranges.

The values in the cells are alphanumeric field names such as COLCOD, A15OB, ACCTNO etc.

The goal if not clear (the worksheet that this column is in has roughly 400 rows) is to count the unique instances of fields in the entire column BUT considering one cell in that column may have several fields as noted above. More specifically one cell might have:

COLCOD
A15OB
ACCTNO

In the above instance I need to return a unique count of 3. But as noted, those same fields could and will be referenced in other cells in the same column, and if those were the only fields, then the unique count of the column of cells would also be 3.

Currently, the fields are not separated by commas and though they happen to lie vertically in the column, that is simply due to the column width, so I may I need to use commas to help delineate a change in fields.

Greatly appreciate the help lads. I have found there to be plenty of brilliant people on this board and I know I can't be the first to have uncovered this need.

Finally, I would prefer the solution be contained within Excel functions since if I introduce VBA I believe it will impair my ability to share the sheet and results with several people that the worksheet gets shared with.

Thanks!

Philo

 

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.
Philometis,

1. What version of Excel and Windows are you using?

2. Are you using a PC or a Mac?


I would think that you will need a macro to get a unique count, or, a unique list.


I do not think that a screenshot of your worksheet will help in this situation.

In your workbook also complete the area in the worksheet, or, in another worksheet, what the results should look like (manually completed by you).

So that we can get it right the first time:

You can upload your workbook/worksheet(s) to Box Net,

sensitive data changed

mark the workbook for sharing

and provide us with a link to your workbook.
 
Last edited:
Upvote 0
I'm working thru the process of loading a sample file to Box Net, and though I have cut my file down to two line items, the uploading of the file has been somewhat problematic (since I have not used it before), so to avoid delays, can I send you the sample file directly?

Thanks so much!
 
Upvote 0
Welcome to the Board!

We try to discourage taking things off the board, as this site is primarily a tool to help others learn and if the conversation goes offline then someone else who might be in a similar position would miss out on a possible solution.

As for the question at hand, have you thought of using Data-->Text to Columns to parse the data?
 
Upvote 0
Philometis,

can I send you the sample file directly?

No.

If you are not able to provide the workbook/worksheet(s), so that we can get it right the first time, then:

Click on the Reply to Thread button, and just put the word BUMP in the thread. Then, click on the Post Quick Reply button, and someone else will assist you.
 
Upvote 0
Unique count
COLCOD
A15OB
ACCTNO

5
COLCOD
ACCTNO

A15OB
ACCTNO
GAD
JAD


<TBODY>
</TBODY>

Install the following function codes by Dunn, Williams, and Grove in a module using Alt+F11...

Rich (BB code):
Public Function V(Optional vrnt As Variant) As Variant
'
' Stephen Dunn
' 2002-09-12
'
Static vrntV As Variant
If Not IsMissing(vrnt) Then vrntV = vrnt
V = vrntV
End Function

Public Function EVAL(theInput As Variant) As Variant
'
' if UDF evaluate the input string as though it was on this sheet
' else evaluate for activesheet
'
Dim vEval As Variant
Application.Volatile
On Error GoTo funcfail
If Not IsEmpty(theInput) Then
If TypeOf Application.Caller.Parent Is Worksheet Then
vEval = Application.Caller.Parent.Evaluate(CStr(theInput))
Else
vEval = Application.Evaluate(CStr(theInput))
End If
If IsError(vEval) Then
EVAL = CVErr(xlErrValue)
Else
EVAL = vEval
End If
End If
Exit Function
funcfail:
EVAL = CVErr(xlErrNA)
End Function

Function aconcat(a As Variant, Optional sep As String = "") As String
' Harlan Grove, Mar 2002
Dim y As Variant
If TypeOf a Is Range Then
For Each y In a.Cells
aconcat = aconcat & y.Value & sep
Next y
ElseIf IsArray(a) Then
For Each y In a
aconcat = aconcat & y & sep
Next y
Else
aconcat = aconcat & a & sep
End If
aconcat = Left(aconcat, Len(aconcat) - Len(sep))
End Function

Excel should have these generic unctions in native form...

Once these are added...

C2, control+shift+enter, not just enter:

Rich (BB code):
=SUM(IF(FREQUENCY(IF(V(EVAL("{"&CHAR(34)&SUBSTITUTE(SUBSTITUTE(REPLACE(aconcat(SUBSTITUTE(";"&$A$2:$A$6,CHAR(10),";")),1,1,""),";",CHAR(34)&";"),";",";"&CHAR(34))&CHAR(34)&"}"))<>"",MATCH(V(),V(),0)),ROW(INDIRECT("1:"&COUNTA(V())))),1))
 
Last edited by a moderator:
Upvote 0
Philometis,

Sample raw data:


Excel 2007
ABC
1COLCOD A15OB ACCTNO
2COLCOD ACCTNO
3A15OB
4ACCTNO
5GAD JAD
6
Sheet1


After the macro:


Excel 2007
ABC
1COLCOD A15OB ACCTNOUnique Count
2COLCOD ACCTNO5
3A15OB
4ACCTNO
5GAD JAD
6
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub GetUniqueCount()
' hiker95, 09/06/2014
Dim c As Range, n As Long, s, i As Long
Application.ScreenUpdating = False
With CreateObject("Scripting.Dictionary")
  .CompareMode = vbTextCompare
  For Each c In Range("A1", Range("A" & Rows.Count).End(xlUp))
    If InStr(c, vbLf) Then
      s = Split(c, vbLf)
      For i = LBound(s) To UBound(s)
        If Not .exists(s(i)) Then
          .Add s(i), 1
          n = n + 1
        End If
      Next i
    Else
      If Not .exists(c.Value) Then
        .Add c, 1
        n = n + 1
      End If
    End If
  Next c
End With
With Cells(1, 3)
  .Value = "Unique Count"
  .Font.Bold = True
End With
Cells(2, 3) = n
Columns(3).AutoFit
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

hen run the GetUniqueCount macro.
 
Upvote 0
Brilliant! Works like a charm. You've left me with some homework in order to fully understand it! Thanks, Aladin!
 
Upvote 0
Hi,

You can do this entirely using worksheet functions alone, but first I'd need to know what precisely separates each of the strings in a cell.

It looks like a genuine linebreak (achieved by pressing ALT+ENTER). Is this in fact the case? Or have you just chosen to display your values like that for the purpose of this post? If so, what precisely does separate the strings within each cell?

Regards
 
Upvote 0
Another brilliant and perfect response. Thanks so much hiker95! You guys are leaving me with some homework! :)
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,438
Members
449,083
Latest member
Ava19

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