Extract data from cell

PresidentEvil

New Member
Joined
Jan 2, 2021
Messages
34
Office Version
  1. 2021
  2. 2016
Platform
  1. Windows
Hi,

I have been working on a VBA macro in outlook that takes input from user and update the excel sheet with the user inputs along with the email subject line and body.

Emails are not in a specific format. Subject line and body (ofcourse) are dynamic.

I want something that can fetch the data from the subject line data that is saved in excel sheet.

Let's say, I have subject line as "Hello, this is a test xxxxxxxxxxWCxx"

I want to extract that last part highlighted (let's call it ID for now) into new row.

This ID can be anywhere in the subject, maybe in the middle or at the beginning or at the end (just like example above). There is also a possibility of having hyphen in that ID. Ex: xxxxx-xxxxx-WC-xx

Can someone please help me with a formula to extract this ID? I tried Right, left and mid formula, but it won't always work.

Any help is highly appreciated!
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
MrExcelPlayground12.xlsx
AB
2I am a silly XxxxxxxxxxxWCxxXxxxxxxxxxxWCxx
3he is Xxxxx-xxxxx-WC-xx at thatXxxxx-xxxxx-WC-xx
4Xxxxx-xxxxx-WC-xx at thatXxxxx-xxxxx-WC-xx
Sheet19
Cell Formulas
RangeFormula
B2:B4B2=TRIM(MID(A2,MAX(SEARCH("WC",A2)-LEN(TRIM(RIGHT(SUBSTITUTE(TRIM(LEFT(A2,SEARCH("WC",A2)))," ", REPT(" ", 99)), 99))),1),IFERROR(SEARCH(" ",A2,SEARCH("WC",A2)),LEN(A2))-MAX(SEARCH("WC",A2)-LEN(TRIM(RIGHT(SUBSTITUTE(TRIM(LEFT(A2,SEARCH("WC",A2)))," ", REPT(" ", 99)), 99))),1)+1))


Here it is, spread out a bit for greater understanding:
MrExcelPlayground12.xlsx
ABCDEF
2I am a silly XxxxxxxxxxxWCxxXxxxxxxxxxxWCxx251328XxxxxxxxxxxWCxx
3he is Xxxxx-xxxxx-WC-xx at thatXxxxx-xxxxx-WC-xx19624Xxxxx-xxxxx-WC-xx
4Xxxxx-xxxxx-WC-xx at thatXxxxx-xxxxx-WC-xx13118Xxxxx-xxxxx-WC-xx
Sheet19
Cell Formulas
RangeFormula
B2:B4B2=TRIM(MID(A2,MAX(SEARCH("WC",A2)-LEN(TRIM(RIGHT(SUBSTITUTE(TRIM(LEFT(A2,SEARCH("WC",A2)))," ", REPT(" ", 99)), 99))),1),IFERROR(SEARCH(" ",A2,SEARCH("WC",A2)),LEN(A2))-MAX(SEARCH("WC",A2)-LEN(TRIM(RIGHT(SUBSTITUTE(TRIM(LEFT(A2,SEARCH("WC",A2)))," ", REPT(" ", 99)), 99))),1)+1))
C2:C4C2=SEARCH("WC",A2)
D2:D4D2=MAX(C2-LEN(TRIM(RIGHT(SUBSTITUTE(TRIM(LEFT(A2,C2))," ", REPT(" ", 99)), 99))),1)
E2:E4E2=IFERROR(SEARCH(" ",A2,C2),LEN(A2))
F2:F4F2=TRIM(MID(A2,D2,E2-D2+1))
 
Upvote 0
MrExcelPlayground12.xlsx
AB
2I am a silly XxxxxxxxxxxWCxxXxxxxxxxxxxWCxx
3he is Xxxxx-xxxxx-WC-xx at thatXxxxx-xxxxx-WC-xx
4Xxxxx-xxxxx-WC-xx at thatXxxxx-xxxxx-WC-xx
Sheet19
Cell Formulas
RangeFormula
B2:B4B2=TRIM(MID(A2,MAX(SEARCH("WC",A2)-LEN(TRIM(RIGHT(SUBSTITUTE(TRIM(LEFT(A2,SEARCH("WC",A2)))," ", REPT(" ", 99)), 99))),1),IFERROR(SEARCH(" ",A2,SEARCH("WC",A2)),LEN(A2))-MAX(SEARCH("WC",A2)-LEN(TRIM(RIGHT(SUBSTITUTE(TRIM(LEFT(A2,SEARCH("WC",A2)))," ", REPT(" ", 99)), 99))),1)+1))


Here it is, spread out a bit for greater understanding:
MrExcelPlayground12.xlsx
ABCDEF
2I am a silly XxxxxxxxxxxWCxxXxxxxxxxxxxWCxx251328XxxxxxxxxxxWCxx
3he is Xxxxx-xxxxx-WC-xx at thatXxxxx-xxxxx-WC-xx19624Xxxxx-xxxxx-WC-xx
4Xxxxx-xxxxx-WC-xx at thatXxxxx-xxxxx-WC-xx13118Xxxxx-xxxxx-WC-xx
Sheet19
Cell Formulas
RangeFormula
B2:B4B2=TRIM(MID(A2,MAX(SEARCH("WC",A2)-LEN(TRIM(RIGHT(SUBSTITUTE(TRIM(LEFT(A2,SEARCH("WC",A2)))," ", REPT(" ", 99)), 99))),1),IFERROR(SEARCH(" ",A2,SEARCH("WC",A2)),LEN(A2))-MAX(SEARCH("WC",A2)-LEN(TRIM(RIGHT(SUBSTITUTE(TRIM(LEFT(A2,SEARCH("WC",A2)))," ", REPT(" ", 99)), 99))),1)+1))
C2:C4C2=SEARCH("WC",A2)
D2:D4D2=MAX(C2-LEN(TRIM(RIGHT(SUBSTITUTE(TRIM(LEFT(A2,C2))," ", REPT(" ", 99)), 99))),1)
E2:E4E2=IFERROR(SEARCH(" ",A2,C2),LEN(A2))
F2:F4F2=TRIM(MID(A2,D2,E2-D2+1))
Thank you, that's what I was looking for.

One final requirement, can that formula be altered to have the final outcome to be in xxxxxxxxxxWCxx and not xxxxx-xxxxx-wc-xx?

Some of the subject line is without hypen,but the one with hypen should not be with hypen after formula
 
Upvote 0
SUBSTITUTE will work for that.
MrExcelPlayground12.xlsx
AB
2I am a silly XxxxxxxxxxxWCxxXxxxxxxxxxxWCxx
3he is Xxxxx-xxxxx-WC-xx at thatXxxxxxxxxxWCxx
4Xxxxx-xxxxx-WC-xx at thatXxxxxxxxxxWCxx
Sheet19
Cell Formulas
RangeFormula
B2:B4B2=SUBSTITUTE(TRIM(MID(A2,MAX(SEARCH("WC",A2)-LEN(TRIM(RIGHT(SUBSTITUTE(TRIM(LEFT(A2,SEARCH("WC",A2)))," ", REPT(" ", 99)), 99))),1),IFERROR(SEARCH(" ",A2,SEARCH("WC",A2)),LEN(A2))-MAX(SEARCH("WC",A2)-LEN(TRIM(RIGHT(SUBSTITUTE(TRIM(LEFT(A2,SEARCH("WC",A2)))," ", REPT(" ", 99)), 99))),1)+1)),"-","")
 
Upvote 0
Solution
SUBSTITUTE will work for that.
MrExcelPlayground12.xlsx
AB
2I am a silly XxxxxxxxxxxWCxxXxxxxxxxxxxWCxx
3he is Xxxxx-xxxxx-WC-xx at thatXxxxxxxxxxWCxx
4Xxxxx-xxxxx-WC-xx at thatXxxxxxxxxxWCxx
Sheet19
Cell Formulas
RangeFormula
B2:B4B2=SUBSTITUTE(TRIM(MID(A2,MAX(SEARCH("WC",A2)-LEN(TRIM(RIGHT(SUBSTITUTE(TRIM(LEFT(A2,SEARCH("WC",A2)))," ", REPT(" ", 99)), 99))),1),IFERROR(SEARCH(" ",A2,SEARCH("WC",A2)),LEN(A2))-MAX(SEARCH("WC",A2)-LEN(TRIM(RIGHT(SUBSTITUTE(TRIM(LEFT(A2,SEARCH("WC",A2)))," ", REPT(" ", 99)), 99))),1)+1)),"-","")
Perfect!
 
Upvote 0

Forum statistics

Threads
1,215,377
Messages
6,124,598
Members
449,174
Latest member
chandan4057

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