Solved

COUNTIF - two speadsheets in argument

Posted on 2014-01-19
13
262 Views
Last Modified: 2014-01-20
Hello,

I currently have a formula as below.

=IF(COUNTIF(January!$A:$X,A138)>0,"Yes","No")

I have created another spreadsheet called February - is there a way I can add this spreadsheet to the argument so that it will display Yes or No if A138 is on either the January or February worksheet?

Thank you in advance,

GISVPN
0
Comment
Question by:gisvpn
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 34

Assisted Solution

by:Dan Craciun
Dan Craciun earned 200 total points
ID: 39791880
=IF(COUNTIF(January!$A:$X,A138) * COUNTIF(February!$A:$X,A138)>0,"Yes","No")

HTH,
Dan
0
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 200 total points
ID: 39791884
The formula above will work if A138 is in both January and February sheets.

If you need it to be in January OR February, try this:

=IF(OR(COUNTIF(January!$A:$X,A138)>0, COUNTIF(February!$A:$X,A138)>0),"Yes","No")
0
 

Author Comment

by:gisvpn
ID: 39791981
Thank you for the posts will try this. Can I can more arguments to this as I go ? For example another one for March?
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39792424
Yup. OR accepts more arguments. For March this would be:

=IF(OR(COUNTIF(January!$A:$X,A138)>0, COUNTIF(February!$A:$X,A138)>0, COUNTIF(March!$A:$X,A138)>0, ),"Yes","No")

and so on...
0
 
LVL 42

Assisted Solution

by:Rob Jurd, EE MVE
Rob Jurd, EE MVE earned 200 total points
ID: 39792907
(no points)
Dan is right on the money, just wanted to point out a trailing comma that's not needed

=IF(OR(COUNTIF(January!$A:$X,A138)>0, COUNTIF(February!$A:$X,A138)>0, COUNTIF(March!$A:$X,A138)>0, ),"Yes","No")

should be

=IF(OR(COUNTIF(January!$A:$X,A138)>0, COUNTIF(February!$A:$X,A138)>0, COUNTIF(March!$A:$X,A138)>0),"Yes","No")
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39792937
Yup, copy/pasted it and forgot to delete the comma.

Thanks Rob!
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 42

Assisted Solution

by:Rob Jurd, EE MVE
Rob Jurd, EE MVE earned 200 total points
ID: 39792962
As another approach you could write your own function as a macro.  The following will loop over all the months and look for the value in A138 if it exists will return "Yes" otherwise "No".  I've attached a demo.  Also will understand if you're wanting to keep it macro free.

Function CalculateCount() As String
    Application.Volatile
    Dim curSheet As Worksheet
    Dim result As String
    
    result = "No"
    
    For Each mth In Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
        If (SheetExists(mth)) Then
            Set curSheet = Sheets(mth)
            If (curSheet.Range("A138").Value > 0) Then
                result = "Yes"
            End If
        End If
    Next mth
    
    Set curSheet = Nothing
    
    CalculateCount = result
End Function

Function SheetExists(ByVal sheetName As String)
    SheetExists = False
    For Each sht In ActiveWorkbook.Worksheets
        If (sht.Name = sheetName) Then
            SheetExists = True
        End If
    Next sht
End Function

Open in new window

Book2.xlsm
0
 
LVL 80

Assisted Solution

by:byundt
byundt earned 100 total points
ID: 39793078
If you might want to search more than two worksheets, then the OR with COUNTIF approach starts growing out of hand. You can avoid that by using this formula with a named range Months that contains the names of the worksheets of interest:
=IF(SUMPRODUCT(COUNTIF(INDIRECT("'" & Months & "'!$A:$X"),A138))>0,"Yes","No")
0
 

Author Comment

by:gisvpn
ID: 39793635
Hello,

@Dan Craciun- thank you for the formular syntax. I am using this currently:

=IF(OR(COUNTIF(January!$A:$X,A138)>0, COUNTIF(February!$A:$X,A138)>0),"Yes","No")

however it incorrectly evaluates to 'Yes' when the value in A138 is not actually on either worksheet? Is there something wrong in my formular - it works perfectly with just January looking for the value.
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39793683
Dan's formula should be:

=IF(OR(COUNTIF(January!$A138,">0")>0, COUNTIF(February!$A138,">0")>0, COUNTIF(March!$A138,">0")>0),"Yes","No")

COUNTIF takes two arguments, the Range and the criteria.  In other words, count the items in A138 that are >0.  If that count is more than 0 then the whole condition returns TRUE.  If any of the COUNTIF statements are TRUE then the formula will show a YES
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39793695
Rob, A138 is a cell, not a range. And the formula says: count the number of times the criteria (A138) is in the range $A:$X.

@gisvpn: can you post a sample file so we see where is the problem? Thank you.
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39793788
Sorry Dan - read it completely wrong, I interpreted incorrectly as each sheet's A138 had a value > 0

for the record though, a cell is still classified as a range
0
 

Author Comment

by:gisvpn
ID: 39793816
Sorry this is actually absolutely fine! It was my mistake the formula was actually correct! :)

Thank you for your help.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
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…

762 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

19 Experts available now in Live!

Get 1:1 Help Now