Substring extraction based on condition

Bering

Board Regular
Joined
Aug 22, 2018
Messages
186
Office Version
  1. 2016
Platform
  1. Windows
Hello,
I would need to find if any string in column T contains the word "FEES" and if so, extract the preceding word plus FEES in the adjacent column.

Example, description in T1:
999-TRANSFER: Y.REF.83237 O.REF.USERINT-MSCI SWFPRCXXXXXPRC TRANSFER OF USD 3,150.00 IN FAVOUR OF ENTITY1 MGT FEES Q2 2023

Result in cell U1:
MGT FEES
If "MGT FEES Q2 2023" was possible that would be even better.

NB, The string FEES can appear anywhere in the description and the length of it is variable.

Example 2:
999-TRANSFER: Y.REF. O.REF.USERINT-HNOL SWFPRC039XXXXXPRC TRANSFER OF USD 24.63 IN FAVOUR OF ENTITY2 VAT FEES 2022 AT A FX RATE OF .92828962 FX PAYMENT EQUIVALENT TO EUR 22.86

The expected result here should be:

VAT FEES
or
even better, VAT FEES 2022

Appreciate any suggestions, thank you!
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Here a UDF with regular expression.

Alt+F11 to open the VBA editor. Insert a new module and paste this code. Afterwards you can use the function like --> =jec(A1)

VBA Code:
Function jec(c As String) As String
 With CreateObject("vbscript.regexp")
   .Global = True
   .Pattern = "([A-Z]+\s)FEES([Q0-9 ]+)"
   If .test(c) Then jec = Trim(.Execute(c)(0))
 End With
End Function


sorteren.xlsx
AB
1999-TRANSFER: Y.REF.83237 O.REF.USERINT-MSCI SWFPRCXXXXXPRC TRANSFER OF USD 3,150.00 IN FAVOUR OF ENTITY1 MGT FEES Q2 2023 bqMGT FEES Q2 2023
2999-TRANSFER: Y.REF. O.REF.USERINT-HNOL SWFPRC039XXXXXPRC TRANSFER OF USD 24.63 IN FAVOUR OF ENTITY2 VAT FEES 2022 B6AT A FX RATE OF .92828962 FX PAYMENT EQUIVALENT TO EUR 22.86VAT FEES 2022
Sheet1
Cell Formulas
RangeFormula
B1:B2B1=jec(A1)
 
Upvote 1
Solution
Here a UDF with regular expression.

Alt+F11 to open the VBA editor. Insert a new module and paste this code. Afterwards you can use the formula like --> =jec(A1)

VBA Code:
Function jec(c As String) As String
 With CreateObject("vbscript.regexp")
   .Global = True
   .Pattern = "([A-Z]+\s)FEES([Q0-9 ]+)"
   If .test(c) Then jec = Trim(.Execute(c)(0))
 End With
End Function


sorteren.xlsx
AB
1999-TRANSFER: Y.REF.83237 O.REF.USERINT-MSCI SWFPRCXXXXXPRC TRANSFER OF USD 3,150.00 IN FAVOUR OF ENTITY1 MGT FEES Q2 2023 bqMGT FEES Q2 2023
2999-TRANSFER: Y.REF. O.REF.USERINT-HNOL SWFPRC039XXXXXPRC TRANSFER OF USD 24.63 IN FAVOUR OF ENTITY2 VAT FEES 2022 B6AT A FX RATE OF .92828962 FX PAYMENT EQUIVALENT TO EUR 22.86VAT FEES 2022
Sheet1
Cell Formulas
RangeFormula
B1:B2B1=jec(A1)
Thank you! I will test it later and let you know 🙏
 
Upvote 0
Try:

varios 07ene2024.xlsm
TU
1999-TRANSFER: Y.REF.83237 O.REF.USERINT-MSCI SWFPRCXXXXXPRC TRANSFER OF USD 3,150.00 IN FAVOUR OF ENTITY1 MGT FEES Q2 2023MGT FEES Q2 2023
2999-TRANSFER: Y.REF. O.REF.USERINT-HNOL SWFPRC039XXXXXPRC TRANSFER OF USD 24.63 IN FAVOUR OF ENTITY2 VAT FEES 2022 AT A FX RATE OF .92828962 FX PAYMENT EQUIVALENT TO EUR 22.86VAT FEES 2022
Hoja5
Cell Formulas
RangeFormula
U1:U2U1=MID(T1,SEARCH("FEES",T1)-4,SEARCH(" ???? ",T1&" ",SEARCH("FEES",T1))-SEARCH("FEES",T1)+9)
 
Upvote 1
Here a UDF with regular expression.

Alt+F11 to open the VBA editor. Insert a new module and paste this code. Afterwards you can use the function like --> =jec(A1)

VBA Code:
Function jec(c As String) As String
 With CreateObject("vbscript.regexp")
   .Global = True
   .Pattern = "([A-Z]+\s)FEES([Q0-9 ]+)"
   If .test(c) Then jec = Trim(.Execute(c)(0))
 End With
End Function


sorteren.xlsx
AB
1999-TRANSFER: Y.REF.83237 O.REF.USERINT-MSCI SWFPRCXXXXXPRC TRANSFER OF USD 3,150.00 IN FAVOUR OF ENTITY1 MGT FEES Q2 2023 bqMGT FEES Q2 2023
2999-TRANSFER: Y.REF. O.REF.USERINT-HNOL SWFPRC039XXXXXPRC TRANSFER OF USD 24.63 IN FAVOUR OF ENTITY2 VAT FEES 2022 B6AT A FX RATE OF .92828962 FX PAYMENT EQUIVALENT TO EUR 22.86VAT FEES 2022
Sheet1
Cell Formulas
RangeFormula
B1:B2B1=jec(A1)
WOW, this works like a charm, thank you very much!
 
Upvote 0
Try:

varios 07ene2024.xlsm
TU
1999-TRANSFER: Y.REF.83237 O.REF.USERINT-MSCI SWFPRCXXXXXPRC TRANSFER OF USD 3,150.00 IN FAVOUR OF ENTITY1 MGT FEES Q2 2023MGT FEES Q2 2023
2999-TRANSFER: Y.REF. O.REF.USERINT-HNOL SWFPRC039XXXXXPRC TRANSFER OF USD 24.63 IN FAVOUR OF ENTITY2 VAT FEES 2022 AT A FX RATE OF .92828962 FX PAYMENT EQUIVALENT TO EUR 22.86VAT FEES 2022
Hoja5
Cell Formulas
RangeFormula
U1:U2U1=MID(T1,SEARCH("FEES",T1)-4,SEARCH(" ???? ",T1&" ",SEARCH("FEES",T1))-SEARCH("FEES",T1)+9)
Thank you very much, it works perfectly if the fees type is of 3 characters.
So if I have Administration fees, the result is ion fees. My bad, this was not clear from my examples.
 
Upvote 0
WOW, this works like a charm, thank you very much!
Hello JEC,
I am truly fascinated by regex now.
Hope you don’t mind me asking some more help…
what if I wanted to be able to also extract “TAX” followed by the pattern ([Q0-9 ]+), in addition to the rest?
In this case I don’t anything preceding the word TAX. Thank you!!
 
Upvote 0
Just a comment in relation to the marked solution.
It won't hurt but I don't see any (reason for)/(value in) the line .Global = True
 
Upvote 0

Forum statistics

Threads
1,224,829
Messages
6,181,218
Members
453,024
Latest member
Wingit77

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