String extraction with a number of conditions?

Rnkhch

Well-known Member
Joined
Apr 28, 2018
Messages
528
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have a column of data with patient visit day information which looks like below:

Day x(text) - y min (text)
Day x(text) - y-z min (text)
Day x(text) - y hr (text)
Day x(text) - y-z hr (text)
Day x(text) - pre (text)
Day x(text) - baseline (text)

where (text) implies that some text may or may not be at those positions.

My goal is to come up with a formula to get the "min" and "hr" numbers, i.e. the numbers immediately preceding "min" and "hr". Also if "pre" or "baseline" are mentioned, I want those words as the output. And if "min", "hr", "pre" or "baseline" are not mentioned, I want an "N/A" output. One issue is that the data is entered sloppily and not all the spaces are necessarily there 😭 Also it would be really nice to get rid of the spaces in between the numbers and the hyphen for hour and min info. Here is an XL2BB sample of inputs and outputs that I need:

Book1
ABC
1InputDesired output
2Day 17N/A
3Day 1 - 0-60 min Post0-60 Min
4Day 1 - prePre
5Day 1 - 7 Hr Post1-7 Hr
6Day 1 - pre(visit1)Pre
7DAY 1(VISIT 1) - 2-4 hr Post2-4 Hr
8DAY 1(VISIT 2) - 0-1 Hr Post(only visit 2)0-1 Hr
9Baseline -Baseline
10DAY 33(VISIT 9) -N/A
11Day12(baseline)Baseline
12Day 11 - 7Hr Post7 Hr
13DAY 23 - 6 - 8 hr Post6-8 Hr
14
Sheet1



Thanks for any input! 🤗
 
Thank you for the feedback
If that resolve the problem then it'd be better to mark the discussion as Resoved; see the procedure: Mark as Solution
 
Upvote 0

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
@Anthony47
It worked very well with my current dataset; thanks much! 🤗 I will let you know in case I need some adjustments if I can't figure out on my own (depending on what I will encounter in other data sets).

@Automatrix
Thank you! I haven't used Power Query yet, but I heard about it a few weeks ago, and I'm excited to try and learn it. If you know a website with a good intro/pointers, please share the link.
I've just used the old method of trying it out and googling whenever I had a specific problem. In this case I would go to the Add Column tab and add conditional columns (eg. if the word "baseline" is there, write "baseline", else nothing) or extractions (e.g. everything before "min"). This will probably generate a lot of helper columns, which can be merged into one column at the end.
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,588
Members
449,039
Latest member
Arbind kumar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top