Separating text String with Name Date of birth and other information

SeliM

Board Regular
Joined
Aug 10, 2023
Messages
64
Office Version
  1. 365
Platform
  1. Windows
Thanks for taking time to read my enquiry.

I am trying to separate a text string output from a dataset that has a combined first name, middle Initial, family name, date of birth, hours and cost structure in one cell. The task is to separate into 6 columns.

I have been experimenting with substitute and textsplit which works fine when the delimiter is "-" and date of birth is using a "/". Example:
George E Smith-26/11/1948-26-1785 the formula result is correct across the 6 columns - first name, middle Initial, family name, date of birth, hours and cost

However, there are entries that use the following a "/" instead of "-" and a "-" instead of a "/". The formula of course separates the date of birth into 3 columns when we require one.

Text: George E Smith/26-11-1948/26/1787 is my challenge. I have tried using different methods for the delimiter but either the name doesn't separate as required or the DoB isn't correctly set out.

Your advice would be most welcomed.
Mel
 
I now have a variety of solutions that help with the various input styles in the data.
This sort of task is notoriously difficult to cover all cases. I was wondering whether you might ever have
  1. Family names consisting of more than one word as in row 7 below?
  2. Family or First names containing a hyphen?
  3. Somebody who does not have a middle name and therefore no middle initial?
My suggested formulas below deal with (at least in some circumstances) points 1 & 3 above, but not point 2.

SeliM.xlsm
ABCDEFG
1
2George E Smith/26-11-1948/26/1787GeorgeESmith26/11/1948261787
3George E Smith/26/11/1948/26/1787GeorgeESmith26/11/1948261787
4George E Smith-26/11/1948-26-1785GeorgeESmith26/11/1948261785
5George E Smith-26-11-1948-26-1785GeorgeESmith26/11/1948261785
6George E Smith-26/11/1948-26/1785GeorgeESmith26/11/1948261785
7Jon J van der Groot-26/11/1948-26/1785JonJvan der Groot26/11/1948261785
8Tim M Smith-Jones-26/11/1948-26/1785TimMSmithJones-26/11/1948261785
9George Smith-26-11-1948-26-1785George Smith26/11/1948261785
Split String (2)
Cell Formulas
RangeFormula
B2:B9B2=LEFT(A2,FIND(" "&IF(C2="","",C2&" "),A2)-1)
C2:C9C2=LET(t,TEXTSPLIT(A2," "),IFNA(INDEX(t,MATCH(1,LEN(t),0)),""))
D2:D9D2=REPLACE(LEFT(A2,MIN(FIND({"-","/"},A2&"-/"))-1),1,LEN(B2&C2)+2-(C2=""),"")
E2:E9E2=LET(s,REPLACE(LEFT(A2,LEN(A2)-LEN(F2&G2)-2),1,MIN(FIND({"-","/"},A2&"-/")),""),IFERROR(--s,s))
F2:G9F2=--TAKE(TEXTSPLIT(A2,{"-","/"}),,-2)
Dynamic array formulas.
 
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Another option might be to utilise a user-defined function like this.

VBA Code:
Function SPLITIT(s As String) As Variant
  Dim RX As Object, M As Object
  Dim a(1 To 6) As Variant
  Dim t As String
  Dim p As Long
  
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  RX.Pattern = "^.+?(?=\/|\-\d)"
  t = RX.Execute(s)(0)
  RX.Pattern = "\b[A-Z]\b"
  If RX.test(t) Then
    p = RX.Execute(s)(0).Firstindex + 1
    a(1) = Left(t, p - 2)
    a(2) = Mid(t, p, 1)
    a(3) = Mid(t, p + 2)
  Else
    a(1) = Split(t)(0)
    a(2) = vbNullString
    a(3) = Split(t, " ", 2)(1)
  End If
  
  RX.Pattern = "(\d{1,2})(\-|\/)(\d{1,2})(\-|\/)(\d{4})"
  Set M = RX.Execute(s)
  a(4) = DateSerial(M(0).Submatches(4), M(0).Submatches(2), M(0).Submatches(0))

  RX.Pattern = "(\/|\-)(\d+)(\/|\-)(\d+)$"
  Set M = RX.Execute(s)
  a(5) = Val(M(0).Submatches(1))
  a(6) = Val(M(0).Submatches(3))
  
  SPLITIT = a
End Function

In the sheet below, the function is entered in B2 and copied down.

SeliM.xlsm
ABCDEFG
1
2George E Smith/26-11-1948/26/1787GeorgeESmith26/11/1948261787
3George E Smith/26/11/1948/26/1787GeorgeESmith26/11/1948261787
4George E Smith-26/11/1948-26-1785GeorgeESmith26/11/1948261785
5George E Smith-26-11-1948-26-1785GeorgeESmith26/11/1948261785
6George E Smith-26/11/1948-26/1785GeorgeESmith26/11/1948261785
7Jon J van der Groot-26/11/1948-26/1785JonJvan der Groot26/11/1948261785
8Bill De Bruyn-26/11/1948-26/1786BillDe Bruyn26/11/1948261786
9Tim M Smith-Jones-26/11/1948-26/1785TimMSmith-Jones26/11/1948261785
10George Smith-26-11-1948-26-1785GeorgeSmith26/11/1948261785
11Jon-Boy Smith-Jones-26/11/1948-26/1785Jon-BoySmith-Jones26/11/1948261785
Split String (3)
Cell Formulas
RangeFormula
B2:G11B2=SPLITIT(A2)
Dynamic array formulas.
 
Upvote 0

Forum statistics

Threads
1,224,978
Messages
6,182,118
Members
453,090
Latest member
boonga

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