Countif in VBA wont work

rubenidas

New Member
Joined
Aug 10, 2016
Messages
11
Hi,
i am having a little trouble with my vba code. I seem to have an error in my countif vba formula. I need B2 to have the formula for countif on range D2 thru last row. The criteria will be D2 (for B2)...D3 (for B3)...D4 (for B4)...and so on.

Code:
Range("B1").Select
    ActiveCell.FormulaR1C1 = "CountIf"
    Range("B2").Formula = "=COUNTIF(D2:D"&lastrow",D2)"
    Range("B2").Select
    Selection.AutoFill Destination:=Range("B2:B" & lastrow)
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
You are missing an & in your formula:
Code:
Range("B2").Formula = "=COUNTIF(D2:D" & lastrow [COLOR=#ff0000][B]&[/B][/COLOR] ",D2)"
 
Upvote 0
Ahhhh! such a small error that caused me a big headache.. Thanks @Joe4 !!
You are welcome!
The devil is often in the details!
 
Upvote 0

Forum statistics

Threads
1,215,518
Messages
6,125,291
Members
449,218
Latest member
Excel Master

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