+ Reply to Thread
Results 1 to 12 of 12

Conditional counting of a string in 2 columns

  1. #1
    Registered User
    Join Date
    06-17-2011
    Location
    The USA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Conditional counting of a string in 2 columns

    Hi!

    I've searched the internet to answer my question but unfortunately had no luck. Maybe you can help me find a solution to my task.

    I know how to count how many times a string appears in a column:
    =(SUM(LEN(A1:A40))-SUM(LEN(SUBSTITUTE(A1:A40,"search string 1",))))/LEN("search string 1")

    But I need to count only if other column (eg. B) contains "search string 2". For example I want to know how many (green, red, golden) "APPLES" were sold to (Miss, missis, mister) "BROWN".

    I had a vague idea about using an asterisk for joining two "SUM" formulae. But I couldn't do it without getting an error.

    Is it possible? I'm sure there is some way.

    Can you please give me a hint how to do it?

    Thanks in advance.
    Last edited by Exboy; 06-18-2011 at 01:44 PM.

  2. #2
    Valued Forum Contributor ron2k_1's Avatar
    Join Date
    09-30-2009
    Location
    Not the USA
    MS-Off Ver
    Excel 2003, 2007
    Posts
    606

    Re: Conditional counting of a string in 2 columns

    would something like this work, given that you have xl2007:
    =COUNTIFS(A:A,"*apple*",B:B,"*brown*")
    Ron
    Knowledge is knowing that a tomato and pepper are fruits. Wisdom is knowing whether to put these in a fruit salad

    Kindly

    [1] Use code tags. Place "[code]" before the first line of code and "[/code"]" after the last line of code. Exclude quotation marks
    [2] Mark your post [SOLVED] if it has been answered satisfactorily by editing your original post in advanced mode.
    [3] Thank (using the little scale) those that provided useful help; its nice and its very well appreciated

  3. #3
    Registered User
    Join Date
    06-17-2011
    Location
    The USA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Conditional counting of a string in 2 columns

    Thanks, but countif counts only cells that 'contain' a string, I have many "apples" and "browns" in a single cell, I need a string search.
    Last edited by Exboy; 06-17-2011 at 03:35 PM.

  4. #4
    Valued Forum Contributor ron2k_1's Avatar
    Join Date
    09-30-2009
    Location
    Not the USA
    MS-Off Ver
    Excel 2003, 2007
    Posts
    606

    Re: Conditional counting of a string in 2 columns

    Give us a dummy workbook with possible cases on a range of cells and then show us what results you want.

  5. #5
    Registered User
    Join Date
    06-17-2011
    Location
    The USA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Conditional counting of a string in 2 columns

    Have no idea what a dummy workbook is, but here is a screenshot:

    \1

  6. #6
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Conditional counting of a string in 2 columns

    Try this array function

    =SUM(IF(ISNUMBER(SEARCH(C2,B1:B40)),(LEN(A1:A40)-LEN(SUBSTITUTE(A1:A40,C1,""))),0))/LEN(C1)
    where C1 contains apple and C2 contains Brown
    Does that work for you? I get 4.
    ChemistB
    My 2?

    substitute commas with semi-colons if your region settings requires
    Don't forget to mark threads as "Solved" (Edit First post>Advanced>Change Prefix)
    If I helped, Don't forget to add to my reputation (click on the little star at bottom of this post)

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  7. #7
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Conditional counting of a string in 2 columns

    Try this, given you are using 2007
    Please Login or Register  to view this content.

    Or, a more generic version using drop-downs
    Please Login or Register  to view this content.


    A dummy workbook is just a sample of your workbook, reduced in size to show typical details, and with any senstitve data disguised. Usually a before and after scenario is useful

    A picture rarely gets an enthusiastic response.

    See the Attached
    Attached Files Attached Files
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  8. #8
    Registered User
    Join Date
    06-17-2011
    Location
    The USA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Conditional counting of a string in 2 columns

    ChemistB & Marcol thank you very much.

  9. #9
    Registered User
    Join Date
    06-17-2011
    Location
    The USA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Conditional counting of a string in 2 columns

    Thanks!

    But, when I entered other data, your formula stopped giving correct results. How do I change it?
    Attached Files Attached Files

  10. #10
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Conditional counting of a string in 2 columns

    Why is your data as it is?
    This is nigh impossible to process as you want, and even if it can be solved, why make life hard?

    It makes much more sense to have each "order" in a seperate line. not concatenated in individual cells.

    What does your real data look like?

  11. #11
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Conditional counting of a string in 2 columns

    I can't see how to do what you are asking for without using VBa

    Try this UDF
    Please Login or Register  to view this content.

    When using this function specify the range by the first column only
    e.g.
    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.

    This is entirely dependent on the adjacent cells in the list having the same number of lines (Alt+Enter) i.e. Chr(10) is used to split the data.

    Hope this helps.
    Attached Files Attached Files

  12. #12
    Registered User
    Join Date
    06-17-2011
    Location
    The USA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Conditional counting of a string in 2 columns

    Quote Originally Posted by Marcol View Post
    Why is your data as it is?

    It makes much more sense to have each "order" in a seperate line. not concatenated in individual cells.

    The problem is, it's the way our .xls file is organized at work and I have no authority to change it.

    P.S. Thanks, your script works!

+ Reply to 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