countif VBA

awsumchillicrab

Board Regular
Joined
Jan 30, 2011
Messages
56
Hi all,

I have a long list of data in column A, many of which repeat themselves.
I want column B to do a countif of the respective entry in column A.

So if "Jacky" appears 3 times in cells A12, A20, and A24,
I should have "3" showing up in B12, B20, and B24.

Also, column A does not always have the same number of rows. I have to use a LastRow coding of some sort which doesn't let me explicitly express the range I want evaluated like A1:A100.

Thank you in advance!
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Try

Code:
Sub test()
Dim LR As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
With Range("B1:B" & LR)
    .Formula = "=COUNTIF(A:A,A1)"
    .Value = .Value
End With
End Sub
 
Upvote 0
Thanks VoG! That works.

Is it possible to do it changing

=countif(A:A, A2)

into something like

=countif(A2:Last Row of A, A2)

Your formula works, but technically covers a bigger range than what I need.
 
Upvote 0
Because of the way that Excel's smart calculation engine works I don't think that the formula would be evaluated beyond the filled range. But anyway, here goes

Code:
Sub test()
Dim LR As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
With Range("B2:B" & LR)
    .Formula = "=COUNTIF(A$2:A$" & LR & ",A2)"
    .Value = .Value
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,750
Members
452,940
Latest member
rootytrip

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