Closed Thread
Results 1 to 10 of 10

COUNTIF two adjacent cells meet logical criteria - help please!

  1. #1
    Registered User
    Join Date
    04-19-2009
    Location
    Swansea, Wales
    MS-Off Ver
    Excel 2007
    Posts
    2

    COUNTIF two adjacent cells meet logical criteria - help please!

    Hi!

    I've got a large spreadsheet of text string data (around 500 rows by 20 columns).

    First, I want to count the number of occurrences of a given text string occurring anywhere in the sheet.
    This is easy to do with the COUNTIF statement. No problems...

    Now my question:
    Secondly, I need to count the number of occurrences where a given text string occurs anywhere on the sheet AND also a second (different) given text string occurs in the cell immediately to the right of each cell that contains the first text string.

    This isn't my actual dataset (I'm not a crazed market gardener!) but I hope the following example illustrates what I'm trying to do:

    beans peas tomatoes cabbages
    cabbages lettuce peas beans
    peas tomatoes cabbages lettuce

    I need a function which will look at the entire sheet and COUNTIF ((a cell contains the text string "peas") AND (the cell immediately to its right contains the text string "tomatoes"))
    - in this example the function should return the value "2"

    I've tried using COUNTIF and VLOOKUP nested in various combinations with the logical AND command, but whereas COUNTIF only needs me to specify the entire data range, VLOOKUP seems to require a specific column index number, and my dataset doesn't allow for this.

    Is it possible to do this in Excel 2007?
    If so I'd be really grateful for any help you could give me in writing a function which will do this without me having to re-enter my entire dataset in a different format.

    Many thanks in anticipation

    Dave

  2. #2
    Forum Contributor
    Join Date
    04-16-2009
    Location
    Stellenbosch, South Africa
    MS-Off Ver
    Excel 2003; Excel 2007; Excel 2010; Excel 2013
    Posts
    136

    Re: COUNTIF two adjacent cells meet logical criteria - help please!

    Hello Dave

    Nothing springs to mind using native worksheet functions (although it must be possible), but I can offer you a UDF. Paste this code to a module in your workbook.

    Please Login or Register  to view this content.
    Apply as follows, e.g:

    =COUNTIFALL("Peas","Tomatoes",A1:Z100)

    Where your 1st criteria is "Peas" and the match to be found in the adjacent cell to the right of the hit should be your 2nd criteria e.g "Tomatoes". Search range is A1:Z100. You can change Crit1 and Crit2 to read from cell references if you prefer, e.g:

    =COUNTIFALL(A1,A2,A1:Z100)
    Regards

    Jon (Excel 2003, 2007, 2010, 2013)

  3. #3
    Registered User
    Join Date
    01-30-2009
    Location
    USA
    MS-Off Ver
    Excel 2003, 2007
    Posts
    67

    Re: COUNTIF two adjacent cells meet logical criteria - help please!

    another way....see attachment
    Attached Files Attached Files

  4. #4
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: COUNTIF two adjacent cells meet logical criteria - help please!

    use an offset sum product in another column dragged down then sum the lot i.e
    sumproduct((a2:s2="peas")*(b2:t2="tomatoes")) or as in column Y
    see attached
    Attached Files Attached Files
    Last edited by martindwilson; 04-20-2009 at 05:39 AM.
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  5. #5
    Registered User
    Join Date
    04-19-2009
    Location
    Swansea, Wales
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: COUNTIF two adjacent cells meet logical criteria - help please!

    Dear all

    Many thanks for your prompt & very helpful responses.

    Greatly appreciated, as the thought of having to trawl through the data manually and count the associations was bringing me out in a cold sweat!!

    Must learn to write Visual Basic Macros...looks like a really powerful technique if I can get my head around it...

    Thanks again for all your help

    Dave

  6. #6
    Registered User
    Join Date
    02-13-2011
    Location
    Portland, Oregon
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: COUNTIF two adjacent cells meet logical criteria - help please!

    I'm trying to use wildcards with this and it isn't working as expected.

    For example:
    =CountIfAll(3,"Std3",C14:D37) returns the correct result for my spreadsheet: 2
    but
    =CountIfAll(3,"Std*",C14:D37) returns 0.

    The latter formula should be returning at least a value of 2, I'm not sure what needs to be changed. . .

  7. #7
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: COUNTIF two adjacent cells meet logical criteria - help please!

    drmartell,

    Unfortunately you need to post your question in a new thread, it's against the forum rules to post a question in the thread of another user. If you create your own thread, any advice will be tailored to your situation so you should include a description of what you've done and are trying to do. Also, if you feel that this thread is particularly relevant to what you are trying to do, you can surely include a link to it in your new thread.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  8. #8
    Registered User
    Join Date
    06-06-2017
    Location
    NJ, USA
    MS-Off Ver
    Windows 7
    Posts
    1

    Re: COUNTIF two adjacent cells meet logical criteria - help please!

    I think this might do it... This is from a spreadsheet that i made to count totals from one sheet that has the info listed week by week, but to render the totals in two-week sprints. Basically you are using the offset function then incrementing the columns by counting them using the counta function. So, as you drag the formula across, the counta functions calculates the next set of columns to sum.

    =SUM(
    COUNTIFS(

    OFFSET('2017 PlanDetail'!$F$4,2,(COUNTA($E$26:O$26)*2)):OFFSET('2017 PlanDetail'!$F$4,50,(COUNTA($E$26:O$26)*2)),

    ClientKeyLookUpTable[@Key]),

    COUNTIFS(

    OFFSET('2017 PlanDetail'!$F$4,2,COUNTA($E$26:O$26)*2+1):OFFSET('2017 PlanDetail'!$F$4,50,(COUNTA($E$26:O$26)*2+1)),

    ClientKeyLookUpTable[@Key])

    )

  9. #9
    Registered User
    Join Date
    01-06-2012
    Location
    india
    MS-Off Ver
    excel 2010
    Posts
    1

    Re: COUNTIF two adjacent cells meet logical criteria - help please!

    Hello Jon
    Thank you for the "CountifAll" Code. I am a newbie in VBA.
    I used your code but my table column as an error, so the code formula also gives an error.
    i added the line "on error resume next" but that output is wrong.
    Can you help me fix this!
    Thank you.

  10. #10
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2503 (Windows 11 Home 24H2 64-bit)
    Posts
    90,480

    Re: COUNTIF two adjacent cells meet logical criteria - help please!

    Administrative Note:

    Welcome to the forum.

    We are happy to help, however whilst you feel your request is similar or even the same as this thread, we have a rule that you open your own thread on the issue and do not piggy back another member's thread.

    Please see Forum Rule #1 about hijacking and start a new thread for your query.

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1