vba - Countif not working

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

I want to store apple count in variable,

formula works if I want to store output in range. but fails if I try to store in Variable. plz assist. thanks


applecount = "=COUNTIF(A1:A5,""Apple"")" 'GET Type mismatch.



Rich (BB code):
Sub Countif()

Dim applecount As Long '

'AppleCount with Formula output in Range works
Range("D5").Formula = "=COUNTIF(A1:A5,""Apple"")"
Range("D5").FormulaR1C1 = "=COUNTIF(R1C1:R5C1,""Apple"")"


'How to Store Count in Variable
'AppleCount with Formula output in Variable not working.
applecount = "=COUNTIF(A1:A5,""Apple"")"  'GET Type mismatch.
applecount = "=COUNTIF(R1C1:R5C1,""Apple"")" ''GET Type mismatch.


'This works
applecount = WorksheetFunction.Countif(Range(Cells(2, 1), Cells(10, 1)), "Apple")
MsgBox applecount


End Sub

thanks
mg
 
Thanks once again , this also worked, learned something new today. ?
You're welcome.

In relation to other threads I try to look at as many as I can and if I come across yours and think I can help, I will.
 
Upvote 0

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

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