Macro to count cells for x number

moorej

New Member
Joined
Mar 17, 2003
Messages
30
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
 
When I paste the formula in column B it kinda works but the cell becomes double spaced and I can't get the other numbers to show up. What am I missing?


A 1
A
A 3
A
A 5
A
A 2
A
A 4
A
A 1
A
A 3
A
A 0
A
A 2
A
A 4
A
[/img]
 
Upvote 0

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Hi

if yiou tried one of the formula I presented, which one did you try?

There are 4 formula in that message.

rgds,
jindon
 
Upvote 0
It was =IF(COUNTIF(A$1:A1,A1)<=5,COUNTIF(A$1:A1,A1),MOD(COUNTIF(A$1:A1,A1),5))

But all gave a double spaced cell with some numbers not visible.

Thanks
 
Upvote 0
Hi

I think it is easier with vba

try the code. it will count 1 - 5 repeatedly
Code:
Sub count_5()
Dim lastA As Long, i As Long, x As Integer
x = 5     '<----  alter this number to suite
With ActiveSheet
    .Range("b:b").ClearContents
    lastA = .Range("a65536").End(xlUp).Row
    .Range(.Cells(2, 1), .Cells(lastA, 1)).Sort _
        key1:=.Cells(2, 1), order1:=xlAscending
    .Range("b1") = 1
    For i = 2 To lastA
        If .Cells(i - 1, 1) = .Cells(i, 1) Then
            If .Cells(i - 1, 2) + 1 <= x Then
                .Cells(i, 2) = .Cells(i - 1, 2) + 1
            Else
                .Cells(i, 2) = (.Cells(i - 1, 2).Value + 1) Mod x
            End If
        Else
            .Cells(i, 2) = 1
        End If
    Next
End With
hope this helps
jindon
 
Upvote 0
Works like a dream. Just what I was looking for. Thanks for everyone's help. I just love this site.
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,739
Members
448,989
Latest member
mariah3

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