+ Reply to Thread
Results 1 to 13 of 13

Check a column for certain characters

  1. #1
    Registered User
    Join Date
    02-06-2010
    Location
    Brasília
    MS-Off Ver
    Excel 2007
    Posts
    23

    Check a column for certain characters

    Hi! Happy to have found this forum!

    I have an issue.
    Lets say I have a column which cells are populated with codes like

    BGW
    3UG
    WWU
    1BR
    UUBRG
    WUBRG
    7UUGGRRR

    and so on. As you could see, we'll always have a number from 1 to 20 (or no number at all) followed by those letters (which can be only W,U,B,R or G). I'd like to know if there is a function to count the # of times a particular character occurs in a colum. In the above example, the "U" character appears 8 times. This would be nice to automate part of the filling process.

    Just a little difference: since numbers always come before the letters, I need to treat a number sequence as a single value: 12GWW should not be seen as 1 then 2, but "twelve", like it was a single character. So, if I have this column...

    URR
    12WBBB
    6UBG
    12R
    12WG

    ...then "12" appears 3 times. Nevertheless, B appears four times (3 on line 2 and 1 on line 3.)

    Thanks for any help.

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

    Re: Simple challenge - checking a column for certain characters

    if you use a couple of helper colums see attached number or char to count goes in b1 result in e1
    Attached Files Attached Files
    "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

  3. #3
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Simple challenge - checking a column for certain characters

    Extracting count of alphas is relatively straight forward...

    If we assume your first data set appears in A1:A7 then:

    =SUMPRODUCT(LEN(A1:A7)-LEN(SUBSTITUTE(A1:A7,"U","")))

    which returns 7 (not 8)

    Regards numerics, using your 2nd dataset, let's assume that resides in B1:B5

    if we wished to count both 1 and 12 such that we get 0 and 3 respectively then we'll be better off reverting to a SUM / COUNTIF type approach given we know the number appears only once and all numerics within the string form part of that single number, eg:

    =SUM(COUNTIF(B1:B5,{"1W*","1U*","1B*","1R*","1G*"}))
    would return 0

    =SUM(COUNTIF(B2:B6,{"12W*","12U*","12B*","12R*","12G*"}))
    would return 3

    does that help ?

  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: Check a column for certain characters

    wouldnt
    =SUM(COUNTIF(B2:B6,{"12W*","12U*","12B*","12R*","12G*"}))
    be a tad hard to manage if you now wanted to count 20's?

  5. #5
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Check a column for certain characters

    Not entirely sure I follow Martin but I'd openly concede no inline array approach is flexible

    I guess if you wanted to have the numeric search criteria as cell variables then given the 1-20 assumption perhaps:

    Please Login or Register  to view this content.

    or

    Please Login or Register  to view this content.

    where C1 contains the number of interest be it 1, 6, 12 -- returning 0, 1, 3 respectively (based on B1:B5 dataset)
    Last edited by DonkeyOte; 02-06-2010 at 02:13 PM.

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

    Re: Check a column for certain characters

    i just thought th op meant that he would like to count say 2's eg in
    20ugbw
    2ugbw
    20rbgw
    answer =1
    then count say 20's =2

  7. #7
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Check a column for certain characters

    Yes, that's the assumption made behind all of the suggestions made thus far I think, no ?

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

    Re: Check a column for certain characters

    my bad didnt read your whole post! just trying to avoid sumproduct lol

  9. #9
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Check a column for certain characters

    Just for clarity - attached is my "thinking" however muddled...

    The SUM(COUNTIF approach will work for the numerics just as easily (if not easier) as the SUMPRODUCT if we can assume that as inferred the # is always to be followed by one of a handful of alphas.
    Attached Files Attached Files

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

    Re: Check a column for certain characters

    i like that countif,didnt know it could be done like that!

  11. #11
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,043

    Re: Check a column for certain characters

    I made ok for numbers (with helper column).. But can't get character if it's on beggining or end of the word :-S

    Like U here is 7, and it should be 9
    Attached Files Attached Files
    Last edited by zbor; 02-06-2010 at 05:01 PM.
    Never use Merged Cells in Excel

  12. #12
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Check a column for certain characters

    zbor, as outlined already, given you need to treat each alpha character separately a SUMPRODUCT approach is more logical.

    The COUNTIF approach is viable for the numerics given these exist only once per string (if at all).

  13. #13
    Registered User
    Join Date
    02-06-2010
    Location
    Brasília
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Check a column for certain characters

    Wow, thanks! As I can see, you guys got interested on sorting out the optimum solution for this problem!
    So what I did: I read the posts, each one scarier than the previous; I know Excel is very rich and complex, but the more I read the less I know :P. So I tried working on the first post by Martin. And, with few adjustments, I got what I wanted accomplished! Thanks to you. Fantastic was this approach...
    =LEN(SUBSTITUTE(C4;AL2;"^^"))-LEN(C4) (cells addresses may vary)
    That was what I found hard to elaborate before coming here since I didn't know most of those functions. Then I opened the help file and walked through some of them.

    Thanks!

+ 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