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

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

Philometis,

Thanks for the Private Message.

We have a winner.

Sample raw data (not all 406 rows are shown for brevity):


Excel 2007
A
1
2L.S.D.W. Source Field
3CALREP STATUS ACCTNO IN35A1
4CALREP STATUS LNCUDI
5
6
7
8
9
10
405CALREP STATUS ACCTNO IN35A1
406CALREP STATUS ACCTNO IN35A1
407
408
Sheet1


After the new macro:


Excel 2007
A
1
2L.S.D.W. Source Field
3CALREP STATUS ACCTNO IN35A1
4CALREP STATUS LNCUDI
5
6
7
8
9
10
405CALREP STATUS ACCTNO IN35A1
406CALREP STATUS ACCTNO IN35A1
40773
408
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).

Code:
Sub GetUniqueCount_V3()
' hiker95, 09/09/2014, ME803634
Dim c As Range, n As Long, s, i As Long, lr As Long
Application.ScreenUpdating = False
lr = Cells(Rows.Count, "A").End(xlUp).Row
With CreateObject("Scripting.Dictionary")
  .CompareMode = vbTextCompare
  For Each c In Range("A3:A" & lr)
    If c <> "" Then
      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
    End If
  Next c
End With
With Cells(lr + 1, "A")
  .Value = n
  .Font.Bold = True
  .HorizontalAlignment = xlCenter
End With
Columns("A:A").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

Then run the GetUniqueCount_V3 macro.
 
Upvote 0

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Re: Count Unique Values in a Cell - and the Entire Range that Cell Resides In - SOLVED

Hiker95

You are he master, Hiker95. I'll shall properly digest this after at least one cup of coffee tomorrow. Your patience and experience is sincerely appreciated.

Philo
 
Upvote 0
Re: Count Unique Values in a Cell - and the Entire Range that Cell Resides In - SOLVED

Philometis,

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.
 
Upvote 0

Forum statistics

Threads
1,216,116
Messages
6,128,932
Members
449,480
Latest member
yesitisasport

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