extract text/words between string with wildcard and fix text string

ploel

New Member
Joined
Sep 22, 2020
Messages
2
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
hi
I would like to extract the following words from bank statements that are in a cell:

GIRO BANK RAIFFEISEN SCHWEIZ GENOSSENSCHAFT DORFSTRASSE 99 999999 ALIGDNSLIW CH9812349000003109099 Beatrice Tester Bernstrasse 99 1234 Bern SENDER REFERENCE

From here I would have to extract "Beatrice Tester" and "Bern" (underlined above)

GIRO BANK CH9909990456345323499 Tester Max Noah Zurich SENDER REFERENCE

From here I would have to extract "Tester Max" and "Zurich" (underlined above)

So I always have to extract the 2 words after the IBAN (right from CH*) and last word left from "SENDER". Count of words at beginning (up to IBAN) can differ as well as the IBAN itself differs and the count of words after sender reference as well differs.

Thanks for your help!
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Hi and welcome to MrExcel.

Try the following function:

VBA Code:
Function ExtractWords(sText As String) As String
  Dim b As Variant, d As String, e As String
  Dim i As Long
 
  b = Split(sText, " ")
  For i = 0 To UBound(b)
    If UCase(Left(b(i), 2)) = "CH" And Len(b(i)) > 2 Then
      If IsNumeric(Mid(b(i), 3, 1)) Then
        d = b(i + 1) & " " & b(i + 2)
        Exit For
      End If
    End If
  Next
 
  b = Split(sText, " SENDER")
  e = Trim(Mid(b(0), InStrRev(b(0), " ")))
 
  ExtractWords = d & " " & e
End Function

HOW TO INSTALL UDFs
If you are new to UDFs, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. You can now use ExtractWords just like it was a built-in Excel function. For example:

Libro1
AB
1
2GIRO BANK RAIFFEISEN SCHWEIZ GENOSSENSCHAFT DORFSTRASSE 99 999999 ALIGDNSLIW CH9812349000003109099 Beatrice Tester Bernstrasse 99 1234 Bern SENDER REFERENCEBeatrice Tester Bern
3GIRO BANK CH9909990456345323499 Tester Max Noah Zurich SENDER REFERENCETester Max Zurich
Hoja1
Cell Formulas
RangeFormula
B2:B3B2=ExtractWords(A2)
 
Upvote 0
Hi

Another option

VBA Code:
Function ExtractClient(s As String) As String

With CreateObject("VBScript.RegExp")
    .Pattern = ".*CH\d{19}\s(\w+ \w+).*?(\w+) SENDER.*"
    If .Test(s) Then ExtractClient = .Replace(s, "$1 $2")
End With
End Function
 
Upvote 0
Another option with excel formulas.

Libro1
ABC
1
2GIRO BANK RAIFFEISEN SCHWEIZ GENOSSENSCHAFT DORFSTRASSE 99 999999 ALIGDNSLIW CH9812349000003109099 Beatrice Tester Bernstrasse 99 1234 Bern SENDER REFERENCEBeatrice TesterBern
3GIRO BANK CH9909990456345323499 Tester Max Noah Zurich SENDER REFERENCETester MaxZurich
Hoja1
Cell Formulas
RangeFormula
B2:B3B2=TRIM(LEFT(SUBSTITUTE(MID(A2,SEARCH(" ",A2,SEARCH(" CH",A2)+1)+1,1000)," ",REPT(" ",100)),200))
C2:C3C2=TRIM(RIGHT(SUBSTITUTE(LEFT(A2,SEARCH("SENDER",A2)-2)," ",REPT(" ",100)),100))
 
Upvote 0
Here is another function that should also work...
VBA Code:
Function ExtractWords(ByVal sText As String) As String
  Dim Parts() As String
  Parts = Split(Replace(Replace(sText, Chr(160), " "), "CH", "SENDER"), "SENDER")
  Parts = Split(Trim(Parts(UBound(Parts) - 1)))
  ExtractWords = Parts(1) & " " & Parts(2) & " " & Parts(UBound(Parts))
End Function
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

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