Countif in VBA

sjs87

New Member
Joined
Jul 13, 2007
Messages
11
I'm having trouble with the sub I've created called 'NotCompleteCount.' When I test it, a pop-up appears saying that 'Compile Error: Expected function or variable' referring to the 'If NotCompleteCount > 0 Then' line. What I'm trying to do is to get the message to be different if some of the milestones/deliverables in my spreadsheet are not complete (i.e. the count of items with a green, amber, red, or not started status is greater than one).

Thanks very much in advance for any help and the code relevant code can be found below:

Set o5 = Application.ActiveSheet.OptionButton5
If o5.Value = True Then

If NotCompleteCount > 0 Then
response = MsgBox("You have selected COMPLETE as the overall status of this item. HOWEVER, not all deliverables/milestones are set to COMPLETE. Do you still wish to send?", _
vbYesNo, "Confirm")
If (response = VbMsgBoxResult.vbYes) Then
SendReport
End If

Else: response = MsgBox("You have selected COMPLETE as the overall status of this item. Please confirm that this is correct, all details are complete and you are ready to send" _
, vbYesNo, "Confirm")

If (response = VbMsgBoxResult.vbYes) Then
SendReport
End If
End If
End If


Sub NotCompleteCount()

Count = [CountIf(E30:E42,"green";"red";"amber";"not started")]


End Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hi sjs87

- A Sub does not return values. Only functions do.
- The syntax for Countif is wrong. If you want to count deveral things you have to sypply an array.
- Countif woll return an array in your case. I believe you want the sum of all the counts.

Try:
Code:
Function NotCompleteCount() As Long

NotCompleteCount = [Sum(Countif(E30:E42,{"green";"red";"amber";"not started"}))]

End Function

Remark: Why a function if it's just one statement, why not include it directly in the main code?

Hope this helps
PGC
 
Upvote 0
Hi sjs

Youre very close to perfect on this one.
Your problem is that the sub notcompetecount needs to be a function so it can return a value...... so define it as a function. I asssume it is returning a whole number so try everything the same, and define it:

Code:
Function NotCompleteCount () as long

      NotCompleteCount = [CountIf(E30:E42,"green";"red";"amber";"not started")] 

End Function

Also you might want to force yourself to declare your variables so you generate compile errors that are easier to understand, and dont have crazy bugs because of typos in variable names. There is an option under tools options "require variable declaration" that will write "Option Excplicit" automatically at the top of all your modules for this. Or just write that in yourself.

good luck!
-gilman
 
Upvote 0

Forum statistics

Threads
1,222,246
Messages
6,164,797
Members
451,917
Latest member
WEB78

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