Count unique values

nikhil0311

Board Regular
Joined
May 3, 2013
Messages
200
Office Version
  1. 2013
Platform
  1. Windows
Hello Friends,
I need a formula to show unique count in column F as 0 or 1 based on key present in column E. Note that column E is not sorted.
Given that I have 0.5 million rows data, need any effective formula that will not impact the performance of the excel file.

Thanks a lot in advance to this great forum!!

1696668815517.png
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Try this:

Libro1
ABCDEF
1IDProdLOBStatKeyUni
212recCREACT12recCREACT1
366caCCACT66caCCACT1
412caCREACT12caCREACT1
566caCCACT66caCCACT0
612caCREACT12caCREACT0
712recCREACT12recCREACT0
Hoja2
Cell Formulas
RangeFormula
F2:F7F2=IF(COUNTIF($E$2:E2,E2)=1,1,0)


If you have problems with the performance, then you should try a macro.
 
Upvote 0
Your question is not entirely clear to me. Your sample shows a series of 0 & 1 values in column F but your thread title indicates a count of the unique values is wanted. Also, I'm wondering why F7 is populated with a 1 and not F2?

Given that I have 0.5 million rows data, need any effective formula that will not impact the performance of the excel file.
I doubt that you will find one. For example, with only 100,000 rows with my test data, the formula suggested by Dante took over 4 minutes to calculate the column. The following formula 'only' took 43 seconds. Admittedly my machine is quite old and a newer, more powerful one may be considerably quicker, but I would say the time is still far too slow.
Excel Formula:
=--ISNA(MATCH(E2,E$1:E1,0))

Therefore I am agreeing with Dante that a macro is likely a much better option.

If you actually want all the 0/1 values in the adjacent column, then you might consider this one (0.25 seconds for the same 100,000 rows of data)

VBA Code:
Sub Test1()
  Dim d As Object
  Dim a As Variant
  Dim i As Long

  Set d = CreateObject("Scripting.Dictionary")
  With Range("E2", Range("E" & Rows.Count).End(xlUp))
    a = .Value
    For i = 1 To UBound(a)
      If d.exists(a(i, 1)) Then
        a(i, 1) = 0
      Else
        d(a(i, 1)) = 1
        a(i, 1) = 1
      End If
    Next i
    .Offset(, 1).Value = a
  End With
End Sub

If it is just the count of unique items that you want then try
VBA Code:
Sub Test2()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
  
  Set d = CreateObject("Scripting.Dictionary")
  a = Range("E2", Range("E" & Rows.Count).End(xlUp)).Value
  For i = 1 To UBound(a)
    d(a(i, 1)) = 1
  Next i
  Range("G1").Value = d.Count
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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