+ Reply to Thread
Results 1 to 14 of 14

Escape whitespaces in formula strings

  1. #1
    Registered User
    Join Date
    02-09-2016
    Location
    Hilpoltstein, Germany
    MS-Off Ver
    2015
    Posts
    3

    Question Escape whitespaces in formula strings

    Dear helpful people of this forum.

    Let's assume we have cells that contains strings like "-a -bcd -x -xy -xyz" or any combination of those.
    I now need to count occurrences of say "-x". Currently I'm doing that with a formula like:

    =COUNTIF(RawData!C2:C99999;"*-x*")

    Unfortunately that counts "-x" "-xy" and "-xyz" regardless. So I tried

    =COUNTIF(RawData!C2:C99999;"*-x *")

    but that yields the same result, so I assume the space after "-x " is ignored. Same goes for regular text filters one can use via UI.
    Is there any way to enforce "-x" and only that, like "-xwithatrailingspace"

    Thanks in advance.
    //Carsten

  2. #2
    Forum Expert sourabhg98's Avatar
    Join Date
    10-22-2014
    Location
    New Delhi, India
    MS-Off Ver
    Excel 2007, 2013
    Posts
    1,899

    Re: Escape whitespaces in formula strings

    Please Login or Register  to view this content.
    This should work fine. It is working for me.

    Can you attach a smaple workbook.
    You can do so by clicking on "Go Advanced" button at the bottom of the post and then selecting the "Paper Clip" icon from the tools above.
    Happy to Help

    How to upload excel workbooks at this forum - http://www.excelforum.com/the-water-...his-forum.html

    "I don't get things easily, so please be precise and elaborate"

    If someone's post has helped you, thank by clicking on "Add Reputation" below the post.
    If your query is resolved please mark the thread as "Solved" from the "Thread Tools" above.

    Sourabh

  3. #3
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Escape whitespaces in formula strings

    Post some sample data and tell us what result you expect.
    Biff
    Microsoft MVP Excel
    Keep It Simple Stupid

    Let's Go Pens. We Want The Cup.

  4. #4
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2402
    Posts
    13,448

    Re: Escape whitespaces in formula strings

    Why not just =COUNTIF(RawData!C2:C99999;"*-x")

  5. #5
    Forum Expert sourabhg98's Avatar
    Join Date
    10-22-2014
    Location
    New Delhi, India
    MS-Off Ver
    Excel 2007, 2013
    Posts
    1,899

    Re: Escape whitespaces in formula strings

    Quote Originally Posted by Pepe Le Mokko View Post
    Why not just =COUNTIF(RawData!C2:C99999;"*-x")
    But I think that won't accept anything after "-x".
    So it recognizes "abc-x" but not "abc-x -def"

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

    Re: Escape whitespaces in formula strings

    COUNTIF( range, "*-x *") should work as long as
    1. there's never a "-" right after the x and
    2. the "-x" doesn't occur right at the end (as discussed by Pepe and Sourabhg)
    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 Expert sourabhg98's Avatar
    Join Date
    10-22-2014
    Location
    New Delhi, India
    MS-Off Ver
    Excel 2007, 2013
    Posts
    1,899

    Re: Escape whitespaces in formula strings

    Please Login or Register  to view this content.
    This would cover every situation I think.

  8. #8
    Registered User
    Join Date
    02-09-2016
    Location
    Hilpoltstein, Germany
    MS-Off Ver
    2015
    Posts
    3

    Cool Re: Escape whitespaces in formula strings

    Thanks for all the answers. I think in reading them and in preparing the requested sample I found my error.
    First off all it's not a good idea to directly go for a 100K row sheet with huge variety, one can't really verify the results.

    Anyway, I think I have to go for something like:

    =COUNTIF(A2:A8;"*-x *")+COUNTIF(A2:A8;"*-x")

    as my error seemed to be not understanding how "ends with" comes into play. See attached sample.

    countif.xlsx
    Calculation in row 10 obviously counts too much as it also include row 6 for obvious reason.
    My next try in row 11 fails to cover for cells ending in "-x"
    Only row 12 yields the correct result, so I have to enhance all calculation.
    Sounds reasonable but not really fun.

    Or do you see any issues with that approach?

    Thanks, Carsten

  9. #9
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2403
    Posts
    13,406

    Re: Escape whitespaces in formula strings

    Try array entering this in B2 and filling down to get counts at each row.
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    If you aren’t familiar with array-entered formulae array enter means the formula must be committed from edit mode by simultaneously pressing and holding down Ctrl and Shift while hitting Enter.

    For the total count in column A try array entering this.
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Last edited by FlameRetired; 02-10-2016 at 03:32 AM.
    Dave

  10. #10
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2403
    Posts
    13,406

    Re: Escape whitespaces in formula strings

    BTW the ROW($1:$20) was arbitrary. A larger number might be in order ROW($1:$30) ... or better yet replace the ROW functions with ROW(INDIRECT("1:"&LEN($A2:$A8))).
    That way you will always be certain to catch all the characters in column A like this
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    Array entered again.

  11. #11
    Forum Expert
    Join Date
    02-19-2013
    Location
    India
    MS-Off Ver
    07/16
    Posts
    2,386

    Re: Escape whitespaces in formula strings

    may be below count all instances of -X in c2
    =SUMPRODUCT(--(NOT(ISERR(SEARCH("-X ",A2:A8&" ")))))

    or
    =SUM(COUNTIF(A2:A8,{"*-x *","*-x"}))
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    WANT TO SAY THANKS, HIT ADD REPUTATION (*) AT THE BOTTOM LEFT CORNER OF THE POST

    More we learn about excel, more it shows us, how less we know about it.

    for chemistry
    https://www.youtube.com/c/chemistrybyshivaansh

  12. #12
    Registered User
    Join Date
    02-09-2016
    Location
    Hilpoltstein, Germany
    MS-Off Ver
    2015
    Posts
    3

    Re: Escape whitespaces in formula strings

    Thanks for the additional answers. I will not pretend that I have fully understood the row part, but of course the orginal excel has many more columns, so I'm not sure if that influences the performance. That's already an issue with 100K+ rows. I can not upload the original data, though. Confidential and stuff.

    But I do love the =SUM(COUNTIF(A2:A8,{"*-x *","*-x"})) notation for clarity. Didn't even knew about the curly bracket notation. Way cool.

    Big thumbs up to all of you.

  13. #13
    Forum Expert
    Join Date
    02-19-2013
    Location
    India
    MS-Off Ver
    07/16
    Posts
    2,386

    Re: Escape whitespaces in formula strings

    You are welcome and Thanks for the feedback

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

    I would appreciate if you consider clicking add rep icon below the bottom left corner of the post of all who have been a help to you, this adds to the reputation of user.

  14. #14
    Forum Expert
    Join Date
    02-19-2013
    Location
    India
    MS-Off Ver
    07/16
    Posts
    2,386

    Re: Escape whitespaces in formula strings

    Thanks for the rep

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Disable Escape Key
    By foncesa in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 03-12-2014, 05:51 PM
  2. excel formula to search Multiple strings in several columns and return strings
    By krratna123 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 08-13-2013, 11:20 AM
  3. Replies: 1
    Last Post: 08-13-2013, 08:32 AM
  4. escape causes error
    By DJams in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-19-2012, 06:20 PM
  5. Replies: 0
    Last Post: 03-11-2010, 10:34 AM
  6. Escape key for '
    By icemantj in forum Access Tables & Databases
    Replies: 1
    Last Post: 01-15-2010, 09:10 AM
  7. Escape My Debugger!
    By rydeyz in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-09-2008, 09:18 PM

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