Combine IFERROR with WorksheetFunction.CountIfs

reberryjr

Well-known Member
Joined
Mar 16, 2017
Messages
701
Office Version
  1. 365
Platform
  1. Windows
I have around 75 lines of code similar to the ones below. Each line can result in an error for multiple reasons (basically there's nothing to count or total). I cannot seem to find anything on the web that will show me how to add IFERROR in each line. Essentially, when there's an error b/c there's nothing to count or total, I just want the textbox value to be 0.

VBA Code:
Me.txt_PAppInSLA = WorksheetFunction.CountIfs(mP.Range("P:P"), Me.cobo_Assoc, mP.Range("T:T"), "Approved", mP.Range("AF:AF"), "In SLA", mP.Range("S:S"), ">=" & CDate(Me.txt_Start), mP.Range("S:S"), "<=" & CDate(Me.txt_End))
Me.txt_PConInSLA = WorksheetFunction.CountIfs(mP.Range("P:P"), Me.cobo_Assoc, mP.Range("T:T"), "Conditional Approved", mP.Range("AF:AF"), "In SLA", mP.Range("S:S"), ">=" & CDate(Me.txt_Start), mP.Range("S:S"), "<=" & CDate(Me.txt_End))
Me.txt_PDenInSLA = WorksheetFunction.CountIfs(mP.Range("P:P"), Me.cobo_Assoc, mP.Range("T:T"), "Denied", mP.Range("AF:AF"), "In SLA", mP.Range("S:S"), ">=" & CDate(Me.txt_Start), mP.Range("S:S"), "<=" & CDate(Me.txt_End))
Me.txt_PInSLAGT = WorksheetFunction.CountIfs(mP.Range("P:P"), Me.cobo_Assoc, mP.Range("AF:AF"), "In SLA", mP.Range("S:S"), ">=" & CDate(Me.txt_Start), mP.Range("S:S"), "<=" & CDate(Me.txt_End))

'Calcualtes the count of PRF decisions made outside of SLA.
Me.txt_PAppOutSLA = WorksheetFunction.CountIfs(mP.Range("P:P"), Me.cobo_Assoc, mP.Range("T:T"), "Approved", mP.Range("AF:AF"), "Out of SLA", mP.Range("S:S"), ">=" & CDate(Me.txt_Start), mP.Range("S:S"), "<=" & CDate(Me.txt_End))
Me.txt_PConOutSLA = WorksheetFunction.CountIfs(mP.Range("P:P"), Me.cobo_Assoc, mP.Range("T:T"), "Conditional Approved", mP.Range("AF:AF"), "Out of SLA", mP.Range("S:S"), ">=" & CDate(Me.txt_Start), mP.Range("S:S"), "<=" & CDate(Me.txt_End))
Me.txt_PDenOutSLA = WorksheetFunction.CountIfs(mP.Range("P:P"), Me.cobo_Assoc, mP.Range("T:T"), "Denied", mP.Range("AF:AF"), "Out of SLA", mP.Range("S:S"), ">=" & CDate(Me.txt_Start), mP.Range("S:S"), "<=" & CDate(Me.txt_End))
Me.txt_POutSLAGT = WorksheetFunction.CountIfs(mP.Range("P:P"), Me.cobo_Assoc, mP.Range("AF:AF"), "Out of SLA", mP.Range("S:S"), ">=" & CDate(Me.txt_Start), mP.Range("S:S"), "<=" & CDate(Me.txt_End))

'Calculates the total decisions made.
txt_PAppTotal = Val(Me.txt_PAppInSLA.value) + Val(Me.txt_PAppOutSLA.value)
txt_PConTotal = Val(Me.txt_PConInSLA.value) + Val(Me.txt_PConOutSLA.value)
txt_PDenTotal = Val(Me.txt_PDenInSLA.value) + Val(Me.txt_PDenOutSLA.value)
txt_PGT = Val(Me.txt_PInSLAGT.value) + Val(Me.txt_POutSLAGT.value)
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
CountIfs returns zero if no matching rows are found.

What error do your lines produce?
 
Upvote 0
CountIfs returns zero if no matching rows are found.

What error do your lines produce?
If an Associate that is picked from a combo box isn't found, then I get an "unable to perform CountIf error"
 
Upvote 0
What is happening between making the selection from the combo box and running the posted code?
 
Upvote 0
What is happening between making the selection from the combo box and running the posted code?
Just some sorting. The issue arises if the Associate isn't found, or the decision wasn't found (i.e., they approved some stuff but didn't deny stuff). It'll throw the error at this line:
VBA Code:
Me.txt_PDenInSLA = WorksheetFunction.CountIfs(mP.Range("P:P"), Me.cobo_Assoc, mP.Range("T:T"), "Denied", mP.Range("AF:AF"), "In SLA", mP.Range("S:S"), ">=" & CDate(Me.txt_Start), mP.Range("S:S"), "<=" & CDate(Me.txt_End))
 
Upvote 0
I have very little to go on.

Can you populate the cobo_Assoc combo box with unique values from the mP.Range("P:P") range?
 
Upvote 0
I have very little to go on.

Can you populate the cobo_Assoc combo box with unique values from the mP.Range("P:P") range?
Not an option. Looks like if I throw an On Error Resume Next, it'll give me the 0 I'm looking for in most fields. I'll just do If Then statements for the rest.
 
Upvote 0
I couldn't test it but try this:
VBA Code:
With Application
    Me.txt_PDenInSLA = .IfError(.CountIfs(mP.Range("P:P"), Me.cobo_Assoc, mP.Range("T:T"), _
                        "Denied", mP.Range("AF:AF"), "In SLA", mP.Range("S:S"), ">=" & _
                        CDate(Me.txt_Start), mP.Range("S:S"), "<=" & CDate(Me.txt_End)), _
                        0)
End With
 
Upvote 0

Forum statistics

Threads
1,216,077
Messages
6,128,685
Members
449,463
Latest member
Jojomen56

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