Extract Unique Text from cell

pto160

Active Member
Joined
Feb 1, 2009
Messages
482
Office Version
  1. 365
Platform
  1. Windows
I have this report that I download and export from my ERP system into Excel. I have a column with the name and address in one cell on multiple lines. The name is in line 1. The problem I am having is that the name is repeated on the first line from some of the rows. I want the unique text from the name line, which is on the first line. I want to get rid of the text starting with when the first word repeats itself.
This is a bit tricky because the second time the first word repeats itself, there is no space between the last word of the unique text and the second occurrence of the first word.

Book1
ABC
1NameFormulaDesired Result
2Precision EngineeringPrecision Engineering 3900 Waterloo Steet London WGGE15Precision EngineeringPrecision EngineeringPrecision Engineering
3California Health ClinicCalifornia Health Clinic 45 Upper Town Ave California, CA 94102California Health ClinicCalifornia Health ClinicCalifornia Health Clinic
4Townsend Mousetraps 1460 Sussex Street Sussex WEGG5Townsend MousetrapsTownsend Mousetraps
Sheet1


Is there a way to do this?
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
It is easier to use UDF

VBA Code:
Function GetName(cell As Range) As String

Dim LocEnd&
Dim FirstRow$(), FirstWord$()

FirstRow = Split(cell, vbLf)
FirstWord = Split(FirstRow(0), " ")

LocEnd = InStr(Len(FirstWord(0)), cell, FirstWord(0)) - 1
If Not LocEnd = -1 Then
    GetName = Left(cell.Text, LocEnd)
Else
    GetName = FirstRow(0)
End If

End Function
 
Upvote 0
just for fun...
NameCustom
Precision EngineeringPrecision Engineering 3900 Waterloo Steet London WGGE15Precision Engineering
California Health ClinicCalifornia Health Clinic 45 Upper Town Ave California, CA 94102California Health Clinic
Townsend Mousetraps 1460 Sussex Street Sussex WEGG5Townsend Mousetraps

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table15"]}[Content],
    Index = Table.AddIndexColumn(Source, "Index", 1, 1),
    SplitLF = Table.ExpandListColumn(Table.TransformColumns(Index, {{"Name", Splitter.SplitTextByDelimiter("#(lf)", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Name"),
    Group1 = Table.Group(SplitLF, {"Index"}, {{"Count", each _, type table}}),
    ListFirst = Table.AddColumn(Group1, "Custom", each List.First([Count][Name])),
    SplitSpace = Table.ExpandListColumn(Table.TransformColumns(ListFirst, {{"Custom", Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Custom"),
    Distinct = Table.Distinct(SplitSpace, {"Custom"}),
    TS = Table.AddColumn(Distinct, "Custom.1", each Text.Select([Custom], {"A".."Z"})),
    Length = Table.TransformColumns(TS,{{"Custom.1", Text.Length, Int64.Type}}),
    Filter = Table.SelectRows(Length, each ([Custom.1] = 1)),
    TSC1 = Table.SelectColumns(Filter,{"Index", "Custom"}),
    Group2 = Table.Group(TSC1, {"Index"}, {{"Count", each _, type table}}),
    List = Table.AddColumn(Group2, "Custom", each [Count][Custom]),
    Extract = Table.TransformColumns(List, {"Custom", each Text.Combine(List.Transform(_, Text.From), " "), type text}),
    TSC2 = Table.SelectColumns(Extract,{"Custom"})
in
    TSC2
 
Upvote 0
Wow .... Not familiar with Power Query to be fun ?
 
Upvote 0
This can replace your "helper" column
Book8
AB
1NameFormula
2Precision EngineeringPrecision Engineering 3900 Waterloo Steet London WGGE15Precision Engineering
3California Health ClinicCalifornia Health Clinic 45 Upper Town Ave California, CA 94102California Health Clinic
4Townsend Mousetraps 1460 Sussex Street Sussex WEGG5Townsend Mousetraps
Sheet17
Cell Formulas
RangeFormula
B2:B4B2=IF(LEFT(LEFT(A2,SEARCH(CHAR(10),A2)-1),LEN(LEFT(A2,SEARCH(CHAR(10),A2)-1))/2)=MID(LEFT(A2,SEARCH(CHAR(10),A2)-1),LEN(LEFT(A2,SEARCH(CHAR(10),A2)-1))/2+1,LEN(LEFT(A2,SEARCH(CHAR(10),A2)-1))),LEFT(LEFT(A2,SEARCH(CHAR(10),A2)-1),LEN(LEFT(A2,SEARCH(CHAR(10),A2)-1))/2),LEFT(A2,SEARCH(CHAR(10),A2)-1))
 
Upvote 0
Another UDF method, if you prefer this (no helper column as well).
VBA Code:
Function UniqText(cell As Range) As String
    UniqText = Split(cell.Value, vbLf)(0)
    If Left(UniqText, Len(UniqText) / 2) = Mid(UniqText, Len(UniqText) / 2 + 1) Then UniqText = Left(UniqText, Len(UniqText) / 2)
End Function
Excel Formula:
UniqText(A2)
 
Upvote 0
How about this!

TempO.xlsx
AB
1NameFormula
2Precision EngineeringPrecision Engineering 3900 Waterloo Steet London WGGE15Precision Engineering
3California Health ClinicCalifornia Health Clinic 45 Upper Town Ave California, CA 94102California Health Clinic
4Townsend Mousetraps 1460 Sussex Street Sussex WEGG5Townsend Mousetraps
Sheet9
Cell Formulas
RangeFormula
B2:B4B2=IF(LEFT(LEFT(A2,SEARCH(CHAR(10),A2)-1),LEN(LEFT(A2,SEARCH(CHAR(10),A2)-1))/2)=RIGHT(LEFT(A2,SEARCH(CHAR(10),A2)-1),LEN(LEFT(A2,SEARCH(CHAR(10),A2)-1))/2), LEFT(LEFT(A2,SEARCH(CHAR(10),A2)-1),LEN(LEFT(A2,SEARCH(CHAR(10),A2)-1))/2), LEFT(A2,SEARCH(CHAR(10),A2)-1))
 
Upvote 0
Hi,

Maybe this might work for you:

Book3.xlsx
AC
1NameDesired Result
2Precision EngineeringPrecision Engineering 3900 Waterloo Steet London WGGE15Precision Engineering
3California Health ClinicCalifornia Health Clinic 45 Upper Town Ave California, CA 94102California Health Clinic
4Townsend Mousetraps 1460 Sussex Street Sussex WEGG5Townsend Mousetraps
Sheet713
Cell Formulas
RangeFormula
C2:C4C2=TRIM(LEFT(A2,IFERROR(FIND(LEFT(A2,FIND(" ",A2)-1),A2,FIND(" ",A2))-1,MIN(FIND({0,1,2,3,4}+{0;5},A2&1/17))-1)))


C2 formula copied down.
 
Upvote 0
Too late to edit, updated below:

Book3.xlsx
ABC
1NameDesired Result
2Precision EngineeringPrecision Engineering 3900 Waterloo Steet London WGGE15Precision EngineeringPrecision Engineering
3California Health ClinicCalifornia Health Clinic 45 Upper Town Ave California, CA 94102California Health ClinicCalifornia Health Clinic
4Townsend Mousetraps 1460 Sussex Street Sussex WEGG5Townsend Mousetraps Townsend Mousetraps
5PizzaHut 123 A Street Small Town, CA 12345PizzaHut PizzaHut
Sheet713
Cell Formulas
RangeFormula
B2:B5B2=TRIM(LEFT(A2,IFERROR(FIND(LEFT(A2,FIND(" ",A2)-1),A2,FIND(" ",A2))-1,MIN(FIND({0,1,2,3,4}+{0;5},A2&1/17))-1)))
C2:C5C2=TRIM(LEFT(A2,IFERROR(FIND(LEFT(A2,FIND(" ",A2)-1),A2,FIND(" ",A2))-1,FIND(CHAR(10),A2))))


If data within Name cell may or may not contain carriage return, Char(10), use B2 formula.
If data within Name cell Always contain carriage return, use C2 formula.

Either formula copied down column.
 
  • Like
Reactions: alz
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
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