VBA countif

Tom F

Active Member
Joined
Oct 19, 2002
Messages
263
=countif(d12:d100,”apple”)

How would this statement be coded in VBA changing d100 to the first non-blank cell from the bottom of column d?

Thanks for your time and help,
Tom
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Code:
Dim xRange as Range
Set xRange=Cells(Rows.Count,4).End(xlup)
Set xRange=Range(Range("d12"),xRange)
answer=Application.CountIf(xRange,"apple")
 
Upvote 0
mikericson,

I must have made a mistake for there was a Run-Time error '438'. Object doesn't support this property or method in the third line after I placed a Sub / End Sub around your code and ran it.

Also, I need the 'answer' to appear in cell D6.

Many thanks,
Tom
 
Upvote 0
Perhaps:

Code:
Sub test()
Dim x As Long
x = Range("D" & Rows.Count).End(xlUp).Row
If x < 12 Then x = 12
Range("D6") = Application.WorksheetFunction.CountIf(Range("D12:D" & x), "apple")
End Sub
 
Upvote 0
hi,

I am having a similar problem, how could i do a countif in sheet 5, range G:G counting all the Y and placing them in sheet 1 column D.

Also I have multiple countif's doing the same thing in 5 other worksheets, but they look at different ranges e.g. A:D could i possibly do this in one sub or i have to do different subs for each.
 
Upvote 0

Forum statistics

Threads
1,214,784
Messages
6,121,535
Members
449,037
Latest member
tmmotairi

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