ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   variable as criteria in COUNTIF (https://www.excelbanter.com/excel-programming/388620-variable-criteria-countif.html)

Charles L. Snyder

variable as criteria in COUNTIF
 
Hi

This doesn't work (it runs, but gives a result of 0 instead of the
correct answer) when the user responds with 54161 (for example):

Sub HowMany()
Dim res As String
Dim myinput As String
myinput = InputBox("Enter the CPT Code...")
res = Evaluate("=CountIf(Ops, myinput)")
MsgBox (res)
End Sub

This does work:

Sub HowMany()
Dim res As String
Dim myinput As String
'myinput = InputBox("Enter the CPT Code...")
res = Evaluate("=CountIf(Ops, 54161)")
MsgBox (res)
End Sub

I've searched this group and googled, but can't find why or how to get
the VBA for COUNTIF to accept a variable as a criteria....

Any idea how to get around this ?

Thanks again

C L Snyder


Vergel Adriano

variable as criteria in COUNTIF
 
Charles,

Try it this way:

res = Evaluate("=CountIf(Ops, " & myinput & ")")


I believe this way should also work:

res = WorksheetFunction.CountIf(Range("Ops"), myinput)


--
Hope that helps.

Vergel Adriano


"Charles L. Snyder" wrote:

Hi

This doesn't work (it runs, but gives a result of 0 instead of the
correct answer) when the user responds with 54161 (for example):

Sub HowMany()
Dim res As String
Dim myinput As String
myinput = InputBox("Enter the CPT Code...")
res = Evaluate("=CountIf(Ops, myinput)")
MsgBox (res)
End Sub

This does work:

Sub HowMany()
Dim res As String
Dim myinput As String
'myinput = InputBox("Enter the CPT Code...")
res = Evaluate("=CountIf(Ops, 54161)")
MsgBox (res)
End Sub

I've searched this group and googled, but can't find why or how to get
the VBA for COUNTIF to accept a variable as a criteria....

Any idea how to get around this ?

Thanks again

C L Snyder



Charles L. Snyder

variable as criteria in COUNTIF
 
I think I tried every permutation of "=", "&", etc but that one - it
works perfectly!

Thanks

CLS



All times are GMT +1. The time now is 05:48 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com