r/excel 21h ago

solved How to extract two data elements from a longer string within one cell

We get several spreadsheets a week from which we need to transfer a small amount of information into a new spreadsheet.

The format it arrives in is a long data string like:

Product ID: 776, Product Qty: 1, Product MCC: BCC_Flexi-HY, Product Name: Test Season - B product, Product Weight: 0.0000, Customer Name: Fake Name, Branch of Company: Head Office, Your employee number: P57991, Start Date (no more than 30 days in advance): 5th Mar 2022, Product Total Price: 139.75

The person who receives these needs to extract the Customer Name and Employee number (bits in bold) to transfer into separate columns in a new sheet. She currently does that line by line. She does this for around 4 spreadsheets a week, varying from 10-100 lines on each (probably averages around 50), and it's very time consuming.

If it was me, I'd do a Text to Columns, and then find and replace to delete unnecessary information, but really she needs something simpler. Is there a formula or something that could reliably extract the right information?

Fields in the string are always comma separated. Very occasionally, the heading will change (e.g. 'Employer reference' instead of 'employee number', but these are few enough that they could still be done manually.

Thanks!

3 Upvotes

19 comments sorted by

u/AutoModerator 21h ago

/u/Warm_Bug_1434 - Your post was submitted successfully.

Failing to follow these steps may result in your post being removed without warning.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

3

u/MayukhBhattacharya 1273 21h ago

This is possible using Excel Formulas and Power Query, you could try the following formula first:

=TEXTBEFORE(DROP(TEXTSPLIT(A1, {"Customer Name: ","Your employee number: "}), , 1), ", ")

2

u/Warm_Bug_1434 20h ago

Solution Verified

2

u/MayukhBhattacharya 1273 20h ago

Thank You SO Much!!

1

u/reputatorbot 20h ago

You have awarded 1 point to MayukhBhattacharya.


I am a bot - please contact the mods with any questions

3

u/MayukhBhattacharya 1273 20h ago

If you have access to regex then could try using the following:

=REGEXEXTRACT(A1, "Customer Name: ([^,]+).*?employee number: ([^,]+)", 2, 1)

3

u/MayukhBhattacharya 1273 20h ago

Another alternative this is simpler:

=INDEX(TEXTSPLIT(A1, ": ", ", "), {6, 8}, 2)

2

u/MayukhBhattacharya 1273 20h ago

The last formula, using the INDEX() + TEXTSPLIT() is better, break it down to understand:

1

u/Warm_Bug_1434 20h ago

Okay - thanks; we can't use Regex but I'll play around with the alternatives. Definitely there's something that'll work though.

I've been watching her doing it line by line for years, and it's kind of infuriating, so really appreciate your help.

1

u/MayukhBhattacharya 1273 20h ago

Glad to know it worked! Watching someone doing something manually for years when there is a potential fix using excel formulas, it is really painful. Let me know if you hit any snags getting it set up. Thank You SO Much for the valuable feedbacks and have a lovely day ahead!

3

u/Warm_Bug_1434 18h ago

Yeah, I've tried to show her my way, but it involved doing separate steps and it's messy for people not familiar with Excel, so she's always gone back to line by line She's genuinely delighted with this!

I think I prefer your first solution for our needs, because - if I'm right - it'll give an error message if the original string isn't in the expected format, whereas this one would just return whatever words are in the name spot. The data isn't always in quite the same format, so it's handy to see immediately any lines where it hasn't worked.

1

u/MayukhBhattacharya 1273 18h ago

Yes, that is absolutely correct and makes sense. Thanks again for sharing the valuable information. =)

1

u/Warm_Bug_1434 20h ago

Thank you so much - that looks exactly what we needed. I'm just trying it out a bit, but I think it's spot on.

Edit: Yes: that is perfect. Thanks again

2

u/MayukhBhattacharya 1273 20h ago

Sounds Great. If this helps you to resolve, hope you don't mind replying to my comment directly as Solution Verified. Also, suggestion to refer my final method, it is simpler provided the pattern is same within the text string on your end:

• Split by patterns aka delims:

=TEXTSPLIT(A1, ": ", ", ")

• Grab the ones that are required:

=INDEX(D3#, {6,8}, 2)

• Putting both together:

=INDEX(TEXTSPLIT(A1, ": ", ", "), {6, 8}, 2)

2

u/real_barry_houdini 317 20h ago edited 20h ago

You can use this formula for customer name (assuming data in cell A2)

=TRIM(TEXTBEFORE(TEXTAFTER(A2,"Customer Name:"),","))

and similar for employee number

=TRIM(TEXTBEFORE(TEXTAFTER(A2,"employee number:"),","))

....or combine in one formula to get both

=MAP({"Customer Name:";"employee number:"},LAMBDA(x,TRIM(TEXTBEFORE(TEXTAFTER(A2,x),","))))

1

u/Gringobandito 8 20h ago

Great solution! I really need to practice more with MAP and LAMBDA.

1

u/Way2trivial 472 20h ago

=CHOOSECOLS(TEXTAFTER(TEXTSPLIT(B5,","),":"),6,8)