VBA code that can both count the total number of cells of a group of matching numbers and number them numerically?

ubergreen

New Member
Joined
Jun 13, 2024
Messages
33
Office Version
  1. 2021
I am trying to see if it is possible to create a vba code that would be able to automatically be able to recognize groups under the column "Name" and then both output on each line the total number of lines/rows that particular group occupies and number the rows in that group in ascending order.

Example below.


Screenshot 2024-06-17 130957.jpg
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Do you really need VBA?
This can be done with a few simple formulas.

And will all the names already be "grouped" together like that, or do you need it sorted first?
 
Upvote 0
Do you really need VBA?
This can be done with a few simple formulas.

And will all the names already be "grouped" together like that, or do you need it sorted first?

I don't need it to be done in VBA. I have been trying to figure it out using formulas, but haven't been able to find something yet.

The names will already be grouped, so they won't need to be sorted.
 
Upvote 0
1718655777459.png


Formula for cell B2 and copy down:
Excel Formula:
=COUNTIF(A:A,A2)

Formula for cell C2 and copy down:
Excel Formula:
=IF(A2=A1,C1+1,1)
 
Upvote 0
Solution
You can also use this for C2 without relying on the header row.
Excel Formula:
=COUNTIF($A$2:A2,A2)
 
Upvote 0
You can also use this for C2 without relying on the header row.
Excel Formula:
=COUNTIF($A$2:A2,A2)
Great thing about Excel, often times there are many different ways to accomplish the same task!
:)
 
Upvote 0
View attachment 112811

Formula for cell B2 and copy down:
Excel Formula:
=COUNTIF(A:A,A2)

Formula for cell C2 and copy down:
Excel Formula:
=IF(A2=A1,C1+1,1)

Dang. That was such a simple solution. I was definitely over thinking it. I hadn't used countif before, so I wasn't familiar with that code. Thanks for the help!
 
Upvote 0
You are welcome! Glad we were able to help.

If you have Excel 2021 (which it looks like you do), you also have COUNTIFS, which works the same way but allows for multiple conditions/criteria!
And then there are also SUMIF and SUMIFS function which work similarly, but add up values instead of counting them.
These are really handy functions at times!
 
Upvote 0
You can also use this for C2 without relying on the header row.
Excel Formula:
=COUNTIF($A$2:A2,A2)

I do like your version for column C as well. I'll keep it in my notes in case I come across a need or use for it. Thanks!
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,184
Members
452,615
Latest member
bogeys2birdies

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