Countif on a new inserted column

Ossian13

New Member
Joined
Oct 21, 2016
Messages
46
Hi guys,

i have in column A a registration number, and i want to insert a column to add a countif formula to calculate how many times a number appears in my sheet. I had no problems in doing so but i want to know how to write the code so that the countif formula to stop at the last number in column a instead of going all the way down.

my code so far
Code:
Columns("B:B").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("B2").Select
    ActiveCell.FormulaR1C1 = "=COUNTIFS(A:A,A2)"
Range("B2").Select
    Selection.AutoFill Destination:=Range("B2:B2" & xLastrow)

Regards,
Ossian
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Are you trying to limit the range you are looking up into, or the number of cells you are populating with the formula?

If you wanted to limit the cells you are looking up into, it would look something like this:
Code:
Dim lastRow as Long
lastRow = Cells(Rows.Count,"A").End(xlUp).Row
Range("B2").Formula = "=COUNTIFS(A2:A" & lastRow & ",A2)"

If the later, than maybe something like this:
Code:
Dim lastRow as Long
lastRow = Cells(Rows.Count,"A").End(xlUp).Row
Range("B2:B" & lastRow).Formula = "=COUNTIFS(A:A,A2)"
 
Upvote 0
Code:
Sub test()

    Columns("B:B").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("B2").Select
    ActiveCell.FormulaR1C1 = "=COUNTIFS(C[-1],RC[-1])"
    
    Range("A2").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(0, 1).Select
    Range(Selection, Selection.End(xlUp)).Select
    Selection.FillDown


End Sub
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,209
Members
453,022
Latest member
RobertV1609

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