Kathtg
asked on
CountIfs function to call in formula
I have a function for a countif. It is:
Function myCountIf(rng As Range, criteria) As Long
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Metrics" And ws.Name <> "TFSData" Then
myCountIf = myCountIf + WorksheetFunction.CountIf( ws.Range(r ng.Address ), criteria)
End If
Next ws
End Function
I have a spreadsheet that has a formula in one cell. I call this function by: =myCountIf(B:B,A15). This will go to every sheet in the workbook that is not in the If clause, and get the values that match the value in A15. I need a countifs function. I want the formula to go through each sheet not listed in the If clause, look for the value in column B that matches the A15 cell value of the current sheet And then count the value in the H column of that row.
I tried changing the function myCountifs line in the following way but it did not work:
myCountIfs = myCountIfs + WorksheetFunction.CountIfs (ws.Range( rng.Addres s), criteria, ws.Range(rng.Address), criteria)
I called it by writing: -myCountIfs(B:B,A15,H:H,"1 ")
I get the #VALUE! in the cell. Anyone know what is wrong with it?
Function myCountIf(rng As Range, criteria) As Long
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Metrics" And ws.Name <> "TFSData" Then
myCountIf = myCountIf + WorksheetFunction.CountIf(
End If
Next ws
End Function
I have a spreadsheet that has a formula in one cell. I call this function by: =myCountIf(B:B,A15). This will go to every sheet in the workbook that is not in the If clause, and get the values that match the value in A15. I need a countifs function. I want the formula to go through each sheet not listed in the If clause, look for the value in column B that matches the A15 cell value of the current sheet And then count the value in the H column of that row.
I tried changing the function myCountifs line in the following way but it did not work:
myCountIfs = myCountIfs + WorksheetFunction.CountIfs
I called it by writing: -myCountIfs(B:B,A15,H:H,"1
I get the #VALUE! in the cell. Anyone know what is wrong with it?
ASKER
Thanks, unfortunately that didn't work for me either. I added a parenthesis to the end, and then played with variations to no avail. I still get the #NAME.
I was able to get this working in the formula - where I named the spreadsheets directly:
COUNTIFS(James!B:B,A20,Jam es!H:H,"1" )+COUNTIFS (Michael!B :B,A20,Mic hael!H:H," 1")+COUNTI FS(Komal!B :B,A20,Kom al!H:H,"1" )+COUNTIFS (Trina!B:B ,A20,Trina !H:H,"1")
I'm going to try a variation with this in VBA, maybe I'll have better luck. I'll post if it works.
I was able to get this working in the formula - where I named the spreadsheets directly:
COUNTIFS(James!B:B,A20,Jam
I'm going to try a variation with this in VBA, maybe I'll have better luck. I'll post if it works.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks. I selected it from the function dropdownlist so I know I typed it correctly. My excel has been having weird problems - like my copy,paste stopped working, etc). Let me try a brand new workbook.
ASKER
Workbook change didn't help but I ended up removing the myCountIfs and changing the information in myCountIf to match. It worked!
Here is the final:
Function myCountIf(rng As Range, criteria, rng2 As Range, criteria2 As Variant) As Long
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Metrics" And ws.Name <> "TFSData" And ws.Name <> "TFSBugs" Then
myCountIf = myCountIf + WorksheetFunction.CountIfs (ws.Range( rng.Addres s), criteria, ws.Range(rng2.Address), criteria2)
End If
Next ws
End Function
Thank you thank you!!
Here is the final:
Function myCountIf(rng As Range, criteria, rng2 As Range, criteria2 As Variant) As Long
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Metrics" And ws.Name <> "TFSData" And ws.Name <> "TFSBugs" Then
myCountIf = myCountIf + WorksheetFunction.CountIfs
End If
Next ws
End Function
Thank you thank you!!
You're welcome x 2. You really were on the right track; I just nudged you a bit.
Your function only as two arguments - a range and criteria. Your new function needs to have the option for more, plus it has a different name (plural).
This is just a tweak of your original function; not tested:
Open in new window
Regards,
-Glenn