Solved

CountIfs function to call in formula

Posted on 2014-11-06
6
284 Views
Last Modified: 2014-11-06
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(rng.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.Address), 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?
0
Comment
Question by:Kathtg
  • 3
  • 3
6 Comments
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40426600
Quick answer:
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:

Function myCountIfs(rng As Range, criteria As Variant, Optional rng2 As Range, Optional Criteria2 As Variant) As Long
      Dim ws As Worksheet

      For Each ws In ThisWorkbook.Worksheets
          If ws.Name <> "Metrics" And ws.Name <> "TFSData" Then
              myCountIfs = myCountIfs + WorksheetFunction.CountIfs(ws.Range(rng.Address), criteria, (w2.Range(rng2.Address), Criteria2)
          End If
      Next ws
End Function

Open in new window


Regards,
-Glenn
0
 

Author Comment

by:Kathtg
ID: 40426705
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,James!H:H,"1")+COUNTIFS(Michael!B:B,A20,Michael!H:H,"1")+COUNTIFS(Komal!B:B,A20,Komal!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.
0
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
ID: 40426796
The #NAME error means that the function name is different than what you typed in the cell.  They both need to be myCountifs.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:Kathtg
ID: 40426808
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.
0
 

Author Comment

by:Kathtg
ID: 40426836
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.Address), criteria, ws.Range(rng2.Address), criteria2)
        End If
    Next ws
End Function


Thank you thank you!!
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40426870
You're welcome x 2.  You really were on the right track; I just nudged you a bit.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Boolean help 6 28
Excel copy picture into Outlook email 7 47
Excel VBA When using VLookup 6 29
how to extract the last folder name with formula and UDF 5 11
Lync meeting or Lync conferencing is what many organizations would like to deploy to allow them save money. But companies are now giving up for various reasons, one of which is that they cannot join external meetings (non-federated company meetings)…
User Beware!  This is a rather permanent solution to removing your email from an exchange server.  The only way to truly go back is to have your exchange administrator restore your mailbox from backups.  This is usually the option of last resort.  A…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now