Running count of occurrence in list

vikas_newports

Board Regular
Joined
May 26, 2016
Messages
121
Office Version
  1. 365
Platform
  1. Windows
Hello
I have more than 50000 records in my sheet and I am using the below formula to count the running occurrence of record
Excel Formula:
=COUNTIF($A$2:A2,A2)

but it hangs my system and takes more than 5 min to run the formula
Is there any way to fast this process?

Prefer option is simple excel, the other option is VBA for solution
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
With a formula, the best you could do is a logical test to reduce the load when you have consecutive rows of identical data.
Assuming first formula is going into B2 (the reference to B1 needs to refer to the cell immediately above the first formula).
Excel Formula:
=IF(A2=A1,B1+1,COUNTIF($A$2:A2,A2))
If the data is completely random then this could be counterproductive.
 
Upvote 0
If the results do not need to be dynamic (that is the data in column A is not changing) then try this with a copy of your workbook.

VBA Code:
Sub Number_Items()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
  
  a = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
  Set d = CreateObject("Scripting.Dictionary")
  For i = 1 To UBound(a)
    d(a(i, 1)) = d(a(i, 1)) + 1
    a(i, 1) = d(a(i, 1))
  Next i
  Range("B2").Resize(UBound(a)).Value = a
End Sub
 
Upvote 0
Solution
Thanks @Peter_SSs for the fastest solution. I would use it as a personal macro
... I need to paste special it in the end.
Given the last part above, clearly the macro solution is the fastest. However, if you did want to test a formula solution and you are prepared to use your Excel 365 not Excel 2010, I found this about 10 times faster than either of the formulas using COUNTIF.

Excel Formula:
=XLOOKUP(A2,A$1:A1,B$1:B1,0,0,-1)+1
 
Upvote 0
@Peter_SSs
it is giving #Name? error I input the formula in B2 cell
Edit: Sorry I have not excel 365 right now but in my workplace I will try it.
 
Last edited:
Upvote 0
@Peter_SSs , it is only giving 1 against every record
Formula cell = B2
Are you on Excel 365 now? It will only work there.
Also, it may still take a while to calculate your whole column & display the correct results. Test it on a small sample of say 10 varied values in column A.
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,264
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