Initials in a multiple-word cell

Andresleo47

Board Regular
Joined
Oct 29, 2008
Messages
132
Hi Mr. Excel fellas! Hoping you're doing well.

The following are some of the values that I have in a single column:

332241
4321 - P1 W2 John Smith
Balance for Alvin James Fox
12345 - P10 W4 Carl Sagan
12345

Now, I need to put the initials of the names in the next column, so, the output would be

-
JS
AJF
CS
-

Any thoughts?

This is something I would truly appreciate!

Thanks so much,

AV
 
This does the same thing using VBA.

PQ VBA.xlsm
AB
13310672 - P7 W2 Jean Sebestien Pelletier.J.S.P.
23310674 - P7 W2 Jean Sebestien Pelletier.J.S.P.
33310678 - P7 W2 Natasha Trzebinski.N.T.
43227881 - P1 W3 Jordan HoughtonJ.H.
53326480 - P8 W3 Evaughn HeathE.H.
63344673 - P9 W4 Katelyn YoungK.Y.
73264657 - P4W2 Josee RobitailleJ.R.
83228771 - P1 W3 Jean-Sebastien GirouxJ.G.
93331046 - P9 W2 Eric DiffeyE.D.
103331047 - P8 W4 Eric DiffeyE.D.
11AA3181227 
1245848 
13129379 
14412978 
15117794 
1658702932 
17169217 
1867065 
19412445 
204833 
21117794 
222862117 - P13 W2 Matthew SaddlebackM.S.
232961899 - P02 W1 Hayden MatcheeH.M.
242878264 - P13 W3 Tyler JacquesT.J.
252941274 - P01 W2 Reginald SiddallR.S.
262911422 - P02 W3 Logan GaveyL.G.
272914556 - P12 W4 Kaydence WahsatnowK.W.
282914549 - P12 W4 Ferleen KakeesimF.K.
292886711 - P01 W4 Cynthia SagstvenC.S.
302912954 - P01 W1 Doris LaboucanD.L.
312935906 - P13 W4 Jolene grayJ.G.
322975056 - P05 W1 Liza JeromeL.J.
333012404 - P04 W4 joan McKinleyJ.M.
343030481 - P05 W1 Valencia Del-hierroV.D.
352954286 - Samantha VeltkampS.V.
362980489 - P02 W4 Mary ThomasM.T.
372958912 - P01 W4 Barbara ShindekaB.S.
382981640 - P04 W4 Ashton Aubichon-CorrigalA.A.
393030488 - P05 W1 Valencia Del-hierroV.D.
Sheet2
Cell Formulas
RangeFormula
B1:B39B1=it(A1)


VBA Code:
Function IT(s As String)

If InStr(s, "-") > 0 Then
    s = Trim(Replace(Replace(Replace(Replace(s, "  ", " "), "(", ""), ")", ""), ".", ""))
    Dim SP() As String: SP = Split(Trim(Right(s, Len(s) - InStr(s, " - ") - 2)), " ")
    
    With CreateObject("System.Collections.ArrayList")
        For i = LBound(SP) To UBound(SP)
            If Not SP(i) Like "*[0-9]" Then .Add UCase(Left(SP(i), 1)) & "."
        Next i
        IT = Join(.toArray(), "")
    End With
Else
    IT = vbNullString
End If
End Function
 
Upvote 0

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
It works really well. Thanks Guys... As long as the table is called "Table1", it works... So I will keep it simple... Thanks so much...I wonder if there's a way to call this procedure from a Macro? I have a Macro that prompts the file and opens it...
 
Upvote 0
Here is an optimized version of the function. Should run faster and it also deals with the parentheticals, i.e. 'Frank (huai yang) Sun.' returns 'F.S.' instead of 'F.H.Y.S'.

PQ VBA.xlsm
AB
13310672 - P7 W2 Jean Sebestien Pelletier.J.S.P.
23310674 - P7 W2 Jean Sebestien Pelletier.J.S.P.
33310678 - P7 W2 Natasha Trzebinski.N.T.
43227881 - P1 W3 Jordan HoughtonJ.H.
53326480 - P8 W3 Evaughn HeathE.H.
63344673 - P9 W4 Katelyn YoungK.Y.
73264657 - P4W2 Josee RobitailleJ.R.
83228771 - P1 W3 Jean-Sebastien GirouxJ.G.
93331046 - P9 W2 Eric DiffeyE.D.
103331047 - P8 W4 Eric DiffeyE.D.
11AA3181227 
1245848 
13129379 
14412978 
15117794 
1658702932 
17169217 
1867065 
19412445 
204833 
21117794 
222862117 - P13 W2 Matthew SaddlebackM.S.
232961899 - P02 W1 Hayden MatcheeH.M.
242878264 - P13 W3 Tyler JacquesT.J.
252941274 - P01 W2 Reginald SiddallR.S.
262911422 - P02 W3 Logan GaveyL.G.
272914556 - P12 W4 Kaydence WahsatnowK.W.
282914549 - P12 W4 Ferleen KakeesimF.K.
292886711 - P01 W4 Cynthia SagstvenC.S.
302912954 - P01 W1 Doris LaboucanD.L.
312935906 - P13 W4 Jolene grayJ.G.
322975056 - P05 W1 Liza JeromeL.J.
333012404 - P04 W4 joan McKinleyJ.M.
343030481 - P05 W1 Valencia Del-hierroV.D.
352954286 - Samantha VeltkampA.V.
362980489 - P02 W4 Mary ThomasM.T.
372958912 - P01 W4 Barbara ShindekaB.S.
382981640 - P04 W4 Ashton Aubichon-CorrigalA.A.
393030488 - P05 W1 Valencia Del-hierroV.D.
Sheet2
Cell Formulas
RangeFormula
B1:B39B1=IT(A1)


VBA Code:
Function IT(s As String)
Dim POS As Integer: POS = InStr(s, "-")

If POS > 0 Then
    s = noParen(s)
    s = Trim(Replace(Replace(s, "  ", " "), ".", ""))
    Dim SP() As String: SP = Split(Trim(Right(s, Len(s) - POS)), " ")
    
    For i = LBound(SP) To UBound(SP)
        If Not SP(i) Like "*[0-9]" Then IT = IT & UCase(Left(SP(i), 1)) & "."
    Next i
Else
    IT = vbNullString
End If
End Function

Function noParen(s As String) As String
Dim BA() As Byte: BA = s
Dim b As Boolean: b = True

For i = LBound(BA) + 2 To UBound(BA) - 2 Step 2
    If BA(i + 2) = 40 Or BA(i - 2) = 41 Then b = Not b
    If b Then noParen = noParen & Chr(BA(i))
Next i
End Function
 
Upvote 0
I'm trying the Function... And there are some few records that can't be identified:

Input -> "PT-P Chq 28722405 -" ; Output -> "-.C.-."
Input -> "6911380-Bugg Sondra"; Output -> "1.S."
Input -> "88304744 - P8W2 87`Luciel Lamch"; Output -> "8.L."
Input -> "6911380-Hofer Maria"; Output -> "1.M."

I know this happened because the format is slightly different from the rest, but do you guys have any option to clear these one?

Thanks so much!

 
Upvote 0
This seems to take care of those edge cases.

PQ VBA.xlsm
AB
300PT-P Chq 28722405 -P.C.
3016911380-Bugg SondraB.S.
30288304744 - P8W2 87`Luciel LamchL.L.
3036911380-Hofer MariaH.M.
Sheet2
Cell Formulas
RangeFormula
B300:B303B300=IT(A300)


VBA Code:
Function IT(s As String)
s = Trim(Replace(Replace(Replace(Replace(s, "  ", " "), ".", ""), " - ", "-"), "`", " "))
Dim POS As Integer: POS = InStr(s, "-")

If POS > 0 Then
    If InStr(s, "(") > 0 Then s = noParen(s)
    Dim SP() As String: SP = Split(Trim(Right(s, Len(s) - POS)), " ")
    
    For i = LBound(SP) To UBound(SP)
        If SP(i) Like "*[A-Za-z]*" And Not SP(i) Like "*[0-9]" Then IT = IT & UCase(Left(SP(i), 1)) & "."
    Next i
Else
    IT = vbNullString
End If
End Function

Function noParen(s As String) As String
Dim BA() As Byte: BA = s
Dim b As Boolean: b = True

For i = LBound(BA) + 2 To UBound(BA) - 2 Step 2
    If BA(i + 2) = 40 Or BA(i - 2) = 41 Then b = Not b
    If b Then noParen = noParen & Chr(BA(i))
Next i
End Function
 
Upvote 0
Here is another macro to try. It assumes that data starts in G2 and outputs results to K2 and below.

VBA Code:
Sub GetInitials()
  Dim RX As Object
  Dim a As Variant, b As Variant, itm As Variant
  Dim i As Long
  Dim s As String
  
  Const Pat1 As String = "(^.*\d[^A-Za-z])|(\(.*?\))"
  Const Pat2 As String = "\b[A-Z]"
  
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  a = Range("G2", Range("G" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a), 1 To 1)
  For i = 1 To UBound(a)
    s = UCase(a(i, 1))
    If InStr(s, "-") > 0 Then
      RX.Pattern = Pat1
      s = Replace(Replace(RX.Replace(s, ""), "-", ""), "'", "")
      RX.Pattern = Pat2
      For Each itm In RX.Execute(s)
        b(i, 1) = b(i, 1) & itm
      Next itm
    End If
  Next i
  Range("K2").Resize(UBound(b)).Value = b
End Sub

A sample of a variety of your data and the results of the above code:

Andresleo47.xlsm
GK
1
23310672 - P7 W2 Jean Sebestien Pelletier.JSP
33310678 - P7 W2 Natasha Trzebinski.NT
43227881 - P1 W3 Jordan HoughtonJH
53326480 - P8 W3 Evaughn HeathEH
63344673 - P9 W4 Katelyn YoungKY
73264657 - P4W2 Josee RobitailleJR
83228771 - P1 W3 Jean-Sebastien GirouxJG
9AA3181227
1045848
113012404 - P04 W4 joan McKinleyJM
123030481 - P05 W1 Valencia Del-hierroVD
132954286 - Samantha VeltkampSV
142981640 - P04 W4 Ashton Aubichon-CorrigalAA
153121502 - P09 W2 Myla Sky Stanley OsuthorpeMSSO
163166413 - P10 W4 Sandra NEUFELDSN
173187164 - P12 W3 George etzerzaGE
18CREDIT MEMO EQUITABLE LIFE OF CANADA
1988316376 - P9 W3 Jackie (Jadwiga) Jakinik.JJ
203521802 - P10 W3 Judit Dell'Anno.JD
2181745374 - P6 W1 Jasbir KaurJK
2288211243 - P6 W2 Frank (huai yang) Sun.FS
2388218954 - P6 W1 Ashok Gangwar.AG
24PT-P Chq 28722405 -
256911380-Bugg SondraBS
2688304744 - P8W2 87`Luciel LamchLL
276911380-Hofer MariaHM
Sheet1 (2)
 
Upvote 0
maybe that's enough
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Text = Table.TransformColumnTypes(Source,{{"Column7", type text}}),
    CEW = Table.TransformColumns(Table.AddColumn(Text, "IF", each if Text.Contains([Column7], "W") then Text.AfterDelimiter(Text.From([Column7], "en-GB"), "W") else Text.AfterDelimiter(Text.From([Column7], "en-GB"), "-")),{{"IF", Text.Proper, type text}}),
    TS = Table.AddColumn(CEW, "Initials", each Text.Select([IF],{"A".."Z"})),
    RC = Table.RemoveColumns(TS,{"IF"}),
    Date = Table.TransformColumnTypes(RC,{{"Column5", type date}, {"Column8", type date}})
in
    Date
 
Upvote 0
Hi Lrobbo, I tried the new function (Post # 27), and some other entries were not caught:

Input -> "Opt Rfnd -3473974-Cindy Dawson-E406923172 56530" ; Output -> "3."
Input -> "Opt Rfnd -3477855-Jodi McDonald-E000124296 335226" ; Output -> "3."
Input -> "Opt Rfnd -3487499-Avery Hansen -3031945-58 20432"; Output -> "3.H."
Input -> "Opt Rfnd -81740059-Samantha Davidson -630787490 1"; Output -> "8.D"
Input -> "Opt Rfnd -88305627-Emmanuel Airwele-643826201 1001"; Output -> "8."

I'll try Sandy's solution and Peter's solution. Thanks for all your input!!

Andres
 
Upvote 0

Forum statistics

Threads
1,216,028
Messages
6,128,392
Members
449,445
Latest member
JJFabEngineering

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