# Macro to count cells for x number

#### moorej

##### New Member
I'm not for sure if I could use a formula but would rather have a macro. Let's say I have a column with cell values of either an A, B, or C sorted asending. I would like to count the A's for "x" number and repeat until a change in A. Then count B's for "x" number and repeat, then C's. Something like this. The "x" for each will vary form time to time but I can change the "x" value within the macro.

Any input would be very helpful. Thank you.

A's in one column, numbers in another

A:1
A:2
A:3
A:1
A:2
A:3
A:1

Then B's

B:1
B:2
B:1
B:2
B:1
B:2
B:1

Then C's

C:1
C:2
C:3
C:4
C:5
C:1
C:2
C:3
C:4
C:5
C:1
C:2

### Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

#### texasalynn

##### Well-known Member
A pivot table would give you what you want. Put the letters (A,B,C) in the row, put the numbers in the column, then put the numbers in the data as a count.

HTH
texasalynn

#### moorej

##### New Member
I've never used a pivot table. Can I vary the numbers for each A,B, or C? Where my A's may be 1,2,..5, 1,2,..5 number sequence, and B's may be 1,2,...9, 1,2,...9, number sequence, and C's may be 1,2,...25, 1,2,...25.

Thanks

#### texasalynn

##### Well-known Member
Yes! Give the pivot table a shot as I suggested and see what results.

If you have questions, post back.

HTH
texasalynn

#### moorej

##### New Member

Still not clear on a pivot table. How do I get the numbers in a column on the spreedsheet with the ABC's?

#### texasalynn

##### Well-known Member
Ok - here is a sample:
Book1
ABCD
1CharNumb
2A1
3A2
4A3
5A1
6A2
7A3
8A1
9B1
10B2
11B1
12B2
13B1
14B2
15B1
16C1
17C2
18C3
19C4
20C5
21C1
22C2
23C3
24C4
25C5
26C1
27C2
Sheet1

Then create the pivot table as I directed previously
Book1
ABCDEFG
3CountofNumbNumb
4Char12345GrandTotal
5A3227
6B437
7C3322212
8GrandTotal10842226
Sheet4

Does that help?
texasalynn

#### moorej

##### New Member

The problem is I don't have any numbers beside the ABC's and would like to create them based on an "X" number. I really don't need to count how many A's have a 1 or 2 and so on. I just need a number in the column beside the letter.

Thanks

#### texasalynn

##### Well-known Member
I don't understand? Are the numbers you want next the letter dependant upon something? Do they mean something?

based on an "X" number
What does that mean?

texasalynn

#### moorej

##### New Member
Let's say I have a column with 25 A's, 150 B's, and 500 C's. All in the same column. It is sorted in asending order. In the next column I want to be able to input a number count sequence for each group of ABC's. I may want the A's as 1,2,3,4,5,...1,2,3,4,5 or I may want them as 1,2..1,2..1,2 or maybe 1 through 25. Same for the B's and C's. I just need to get sequence numbers beside the letters in a sequence that I can select.

#### jindon

##### MrExcel MVP
Hi,

if you have your data in Col.A and start from the first row then

in cell B1
Code:
``=COUNTIF(A\$1:A1,A1) or =IF(COUNTIF(A\$1:A2,A2)<=2,COUNTIF(A\$1:A2,A2),IF(MOD(COUNTIF(A\$1:A2,A2),2)=0,2,1)) or =IF(COUNTIF(A\$1:A1,A1)<=5,COUNTIF(A\$1:A1,A1),MOD(COUNTIF(A\$1:A1,A1),5)) or =IF(COUNTIF(A\$1:A1,A1)<=25,COUNTIF(A\$1:A1,A1),MOD(COUNTIF(A\$1:A1,A1),25))``
w-click on the fill-handle, or copy it down

hope this helps

jindon

Replies
4
Views
57
Replies
3
Views
131
Replies
0
Views
163
Replies
18
Views
437
Replies
0
Views
136

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

1,151,837
Messages
5,766,721
Members
425,373
Latest member
ndiejennrrd

### 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.

### Which adblocker are you using?

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

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