Macro To Look At Column C, Count Matching Cells and Put In Column D

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,783
Office Version
  1. 365
Platform
  1. Windows
I have workbooks of 100 of thousands of rows and I need a code please that will count the number of matching cells in column D and put a running count in column D please.

Please note it has to be a code rather than a formula. Thanks

Before

Excel 2010
CD
AUA520D 1030
AUA520D 1030
AUA520D 1030
AUA520D 1035
AUA520D 1035
AUA520D 1035
AUA520D 1035
AUA520D 1035
AUA520D 1035

<tbody>
[TD="align: center"]978[/TD]

[TD="align: right"][/TD]

[TD="align: center"]979[/TD]

[TD="align: right"][/TD]

[TD="align: center"]980[/TD]

[TD="align: right"][/TD]

[TD="align: center"]981[/TD]

[TD="align: right"][/TD]

[TD="align: center"]982[/TD]

[TD="align: right"][/TD]

[TD="align: center"]983[/TD]

[TD="align: right"][/TD]

[TD="align: center"]984[/TD]

[TD="align: right"][/TD]

[TD="align: center"]985[/TD]

[TD="align: right"][/TD]

[TD="align: center"]986[/TD]

[TD="align: right"][/TD]

</tbody>
Sheet1



After

Excel 2010
CD
AUA520D 1030
AUA520D 1030
AUA520D 1030
AUA520D 1035
AUA520D 1035
AUA520D 1035
AUA520D 1035
AUA520D 1035
AUA520D 1035

<tbody>
[TD="align: center"]978[/TD]

[TD="align: right"]1[/TD]

[TD="align: center"]979[/TD]

[TD="align: right"]2[/TD]

[TD="align: center"]980[/TD]

[TD="align: right"]3[/TD]

[TD="align: center"]981[/TD]

[TD="align: right"]1[/TD]

[TD="align: center"]982[/TD]

[TD="align: right"]2[/TD]

[TD="align: center"]983[/TD]

[TD="align: right"]3[/TD]

[TD="align: center"]984[/TD]

[TD="align: right"]4[/TD]

[TD="align: center"]985[/TD]

[TD="align: right"]5[/TD]

[TD="align: center"]986[/TD]

[TD="align: right"]6[/TD]

</tbody>
Sheet1
 
Last edited:

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Try this:

Code:
[FONT=lucida console][COLOR=Royalblue]Sub[/COLOR] a1092265a()
[I][COLOR=seagreen]'https://www.mrexcel.com/forum/excel-questions/1092265-macro-look-column-c-count-matching-cells-put-column-d.html[/COLOR][/I]
[I][COLOR=seagreen]'' section group looping[/COLOR][/I]
[COLOR=Royalblue]Dim[/COLOR] i [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], j [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], k [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], n [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR]
[COLOR=Royalblue]Dim[/COLOR] va
va = Range([COLOR=brown]"C1"[/COLOR], Cells(Rows.count, [COLOR=brown]"C"[/COLOR]).[COLOR=Royalblue]End[/COLOR](xlUp))
[COLOR=Royalblue]For[/COLOR] i = [COLOR=crimson]1[/COLOR] [COLOR=Royalblue]To[/COLOR] UBound(va, [COLOR=crimson]1[/COLOR])
    j = i
    [COLOR=Royalblue]Do[/COLOR]
        i = i + [COLOR=crimson]1[/COLOR]
        [COLOR=Royalblue]If[/COLOR] i > UBound(va, [COLOR=crimson]1[/COLOR]) [COLOR=Royalblue]Then[/COLOR] [COLOR=Royalblue]Exit[/COLOR] [COLOR=Royalblue]Do[/COLOR]
    [COLOR=Royalblue]Loop[/COLOR] [COLOR=Royalblue]While[/COLOR] va(i, [COLOR=crimson]1[/COLOR]) = va(i - [COLOR=crimson]1[/COLOR], [COLOR=crimson]1[/COLOR])
    
    i = i - [COLOR=crimson]1[/COLOR]:    n = [COLOR=crimson]0[/COLOR]
        [COLOR=Royalblue]For[/COLOR] k = j [COLOR=Royalblue]To[/COLOR] i
            n = n + [COLOR=crimson]1[/COLOR]:  va(k, [COLOR=crimson]1[/COLOR]) = n
        [COLOR=Royalblue]Next[/COLOR]

[COLOR=Royalblue]Next[/COLOR]

Range([COLOR=brown]"D1"[/COLOR]).Resize(UBound(va, [COLOR=crimson]1[/COLOR]), [COLOR=crimson]1[/COLOR]) = va

[COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]Sub[/COLOR][/FONT]
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,217
Members
452,619
Latest member
Shiv1198

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