4
I need an Excel formula (or VBA macro) that will allow me to extract a value from a string. The string is a sequence of words, separated by spaces, in a single cell. I want the word (representing a bike size) that is either
- a number (presumably an integer, but this is not specified) between 47 and 60 (or some other range, perhaps to be specified dynamically), or
- one of the strings "sm", "med", or "lg".
I expect that there will be exactly one qualifying word in the string, so any reasonable error-handling response to
- no qualifying words, or
- multiple qualifying words
will be acceptable. The size may be at various positions in the string. Examples:
Cervelo P2 105 5800 56 '15 the number 56 is the desired result Cervelo P2 105 54 6000 '15 the number 54 is the desired result Cervelo P3 105 5800 60 '15 the number 60 is the desired result Cervelo P2 105 5800 sm '15 the string sm is the desired result
I'm interested only in whole words, so 58 (substring of "5800") does not qualify.
Right now I am stripping off the '15
and then extracting the last two digits. But this approach works only if the bike size is the second to last value. However, as shown above, there are cases where the size is at other positions in the string.
How can I do this with a formula or VBA macro in Excel?
1How do you know the number of interest is the 56 and not the 58? What if, as in this case, the string contains more than one match? Every solution I can think of would match the first instance it finds that meets the criteria. Also, you suggest two ranges in the same question. Is the range intended to be entered as variables so it can be changed without modifying formulas everywhere? – fixer1234 – 2015-08-23T18:26:07.430
Thanks for your help. Each record will only have one occurance of – carroll – 2015-08-25T04:33:21.830
a number that is in the range of 44 - 62. These are bike sizes - so if I can define this range(44-62), the formula will only find one number in this range for each record Thanks Carroll – carroll – 2015-08-25T04:37:24.730
Then can you clarify your example? As described, both the 58 and the 56 meet your criteria. It will be hard for people to provide useful answers if they don't fully understand the requirements. – fixer1234 – 2015-08-25T04:39:31.893
(1) You say that this question is about Excel. So, please clarify: is that string a single cell value, with embedded spaces, or is it six cell values? (2) Rather than say, "there are cases where the size is at various positions in the string", please show some additional examples of what the data might look like. (3) And what's the range? You have said 44-62, 47-60, and 47-62. – Scott – 2015-08-25T07:25:45.280
fixer1234 - my original post was answered by jcbermu with a VBA program which works great. I have spent the last two days testing the program and had tried to answer the questions asked by scott and others. Today I tried to get back with jcbermu to say the his VBA solution works great and ask a couple of questions, but the post had been closed. I will try to clarify further; two records might be 1. Cervelo P2 105 54 6000 '15 and 2. Cervelo P3 105 5800 60 '15 - where the bike sizes are 54 and 60. I want a solution that will extract the 54 and 60 and ignore other # hence the range44-62. – carroll – 2015-08-26T06:59:03.570
1“I am … not sure what I did wrong.” Seriously? I asked you to clarify the range, since you had specified multiple values. Not only did you leave both 47-60 and 47-62 in the question, but you added 44-62 (which you had previously mentioned in a comment) to the question. (Also, the example inputs (and corresponding desired results) that you put in a comment today should have been edited into the question, and the remarks that you edited into the question (“I am not sure what to do next. I am also not sure what I did wrong.”) should have posted in comments (or not at all). … (Cont’d) – Scott – 2015-08-26T17:03:42.583
(Also, since the general subject area of a question (e.g., Excel) is identified by the tags, we prefer not to repeat it in the title.) But you’re new to Super User (note, btw, that we refer to it as a two-word name), so it’s understandable that you haven’t mastered all of our subtle rules in four days. I have voted to reopen the question. (It would still be nice if you would clarify the range, and not have conflicting values.) – Scott – 2015-08-26T17:04:51.060
@carroll: The site works a little different from others you might be used to, so a quick response on your comment about jcbermu answering the question. SU's purpose is to build a knowledgebase of questions and answers for other people to reference. In essence, you contribute a question in exchange for possibly getting an answer yourself. What's important is that the question be understandable by others. Your getting an answer is great, but not the driver. Scott did a great job of clearing up the ambiguity and I voted to reopen. – fixer1234 – 2015-08-26T17:48:05.370
fixer1234 - thanks to everyone for their help and suggestions for clarification. Next time I will be able to respond more effectively – carroll – 2015-08-28T16:07:25.267