Countif in VBA showing error in downward motion..

srlakra

New Member
Joined
Mar 24, 2015
Messages
23
Hi,

I have different values in "A" Column duplicates also. I have to get the count of value in A2 from A2:A100 in B2, then count of value in A3 from A3:A100 in B3,,,
count of value in A20 from A20:A100 in B20 and so on.

I worked on it with my code but some error is there I can't find.

Please help....

Dim i As Long
Dim lastusedrow As Long

lastusedrow = Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To lastusedrow
cells(I,"B")=application.WorksheetFunction.CountIf(Range("A"&i&":A"& lastusedrow),"A"&i)

Next I

Thanks & regards,
Satish R Lakra
 
Last edited:

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
You could try:

Code:
Cells(i, "B") = WorksheetFunction.CountIf(Range(Cells(i, "A"), Cells(lastusedrow, "A")), Cells(i, "A"))
 
Upvote 0
Try:
Code:
Sub Macro1()
    
    Dim x       As Long
    Dim strfrm  As String
    
    strfrm = "=COUNTIF(A2:$A$@1,A2)"
    
    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
    End With
    
    With ActiveSheet
        x = .Cells(.Rows.Count, 1).End(xlUp).row
        With .Cells(2, 2).Resize(x - 1)
            .Formula = Replace(strfrm, "@1", x)
            ActiveSheet.Calculate
            .Value = .Value
        End With
    End With
    
    With Application
        .ScreenUpdating = True
        .Calculation = xlCalculationAutomatic
    End With
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,113
Messages
6,128,904
Members
449,477
Latest member
panjongshing

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