Substitute from a list in another sheet

RonOliver

Board Regular
Joined
Aug 30, 2022
Messages
99
Office Version
  1. 365
Platform
  1. Windows
Hi!

I'm trying to get this tool to join the strings of text and then delete certain prepositions from the names (if F2 has the word "Delete"):

Book1
ABCDEF
1JorgeGuzmánPedrazaJorge Guzmán Pedraza
2DavidGarcíaGarcíaDavid García GarcíaDelete
3TamaraBasMorenoTamara Bas Moreno
4MiguelUreñaPérezMiguel Ureña Pérez
5MónicaEstadellasVacaMónica Estadellas Vaca
6LenaViejoAlegreLena Viejo Alegre
7FrancescoSchettinoFrancesco Schettino
8MartaArenasCabrerizoMarta Arenas Cabrerizo
9AuraLópezAlcázarAura López Alcázar
10JavierAlarcónAznarJavier Alarcón Aznar
11NabilHrairaNabil Hraira
12JavierFernándezParraJavier Fernández Parra
13FátimaAndradeBayarriFátima Andrade Bayarri
14NoemiCaravantesFenollosaNoemi Caravantes Fenollosa
15GabrielaLópez-Morenodel PinoGabriela López-Moreno Pino
16Carlosde la TorreRamírezCarlos Torre Ramírez
17Juan AntonioGarcíaGarcíaJuan Antonio García García
18María de los ÁngelesOjedade MorataMaría Ángeles Ojeda Morata
Sheet1
Cell Formulas
RangeFormula
D1:D18D1=IF(AND(ISBLANK(A1:C1))," ",CONCAT(IF($F$2="Delete",IFERROR(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1," de los "," ")," de las "," ")," de la "," ")," del "," ")," de "," ")," el "," ")," la "," ")," los "," ")," las "," ")," y "," ")," i "," ")," dels "," ")," de les "," ")," les "," ")," els "," ")," De Los "," ")," De Las "," ")," De La "," ")," Del "," ")," De "," ")," El "," ")," La "," ")," Los "," ")," Las "," ")," Y "," ")," I "," ")," Dels "," ")," De Les "," ")," Les "," ")," Els "," "),A1),A1)," ",IF($F$2="Delete",TRIM(RIGHT(SUBSTITUTE(B1," ",REPT(" ",100)),100)),B1),IF(ISBLANK(C1),," "),IF($F$2="Delete",TRIM(RIGHT(SUBSTITUTE(C1," ",REPT(" ",100)),100)),C1)))


My problem is each formula has the list of prepositions that I'd like to remove from the resulting string of text, which makes it uncomfortable when I want to add new prepositions to the list. For that reason, I have created an expandable list of prepositions in another sheet (named "Sheet2")

Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAWAXAYAZ
1yYiIdelsDelsde lesDe lesDe LeselsElslesLesde laDe laDe Lade lasDe lasDe Lasde losDe losDe LosdelDeldeDeelEllaLalosLoslasLasaitAitvonVonvanVanvon derVon derVon Dervan derVan derVan DerdellaDelladallaDalladiDi
Sheet2


My question is: how can I fix the formula so I can stop stacking substitute functions and start using a list of prepositions that I can expand if need be? If possible, I need a formula that does exactly the same thing: delete the prepositions from the names. Any suggestions will be appreciated! :)
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Another way, only lightly tested:

ABCD
1de losde ladelde
2
3GabrielaLópez-Morenodel PinoGabriela López-Moreno Pino
4Carlosde la TorreRamírezCarlos Torre Ramírez
5Juan AntonioGarcíaGarcíaJuan Antonio García García
6María de los ÁngelesOjedade MorataMaría Ángeles Ojeda Morata
7AAA de BBBCCC de la DDDde EEEEAaa Bbb Ccc Ddd Eeee
8
Sheet1
Cell Formulas
RangeFormula
D3:D7D3=TEXTJOIN(" ",,DropPrepositions(A3:C3,A$1:D$1))


DropPrepositions: =LAMBDA(s,list,LET(N,COUNTA(list),sNew,SUBSTITUTE(" "&UPPER(TRIM(s))&" "," "&INDEX(UPPER(list),1)&" "," "),IF(N=1,TRIM(PROPER(sNew)),DropPrepositions(sNew,DROP(list,,1)))))

But you need to be careful to get your list in the right order. Here's a wrong way:

ABCD
1dede losde ladel
2
3GabrielaLópez-Morenodel PinoGabriela López-Moreno Pino
4Carlosde la TorreRamírezCarlos La Torre Ramírez
5Juan AntonioGarcíaGarcíaJuan Antonio García García
6María de los ÁngelesOjedade MorataMaría Los Ángeles Ojeda Morata
7AAA de BBBCCC de la DDDde EEEEAaa Bbb Ccc La Ddd Eeee
8
Sheet1
Cell Formulas
RangeFormula
D3:D7D3=TEXTJOIN(" ",,DropPrepositions(A3:C3,A$1:D$1))
 
Upvote 0
Upvote 0
Another way, only lightly tested:

ABCD
1de losde ladelde
2
3GabrielaLópez-Morenodel PinoGabriela López-Moreno Pino
4Carlosde la TorreRamírezCarlos Torre Ramírez
5Juan AntonioGarcíaGarcíaJuan Antonio García García
6María de los ÁngelesOjedade MorataMaría Ángeles Ojeda Morata
7AAA de BBBCCC de la DDDde EEEEAaa Bbb Ccc Ddd Eeee
8
Sheet1
Cell Formulas
RangeFormula
D3:D7D3=TEXTJOIN(" ",,DropPrepositions(A3:C3,A$1:D$1))


DropPrepositions: =LAMBDA(s,list,LET(N,COUNTA(list),sNew,SUBSTITUTE(" "&UPPER(TRIM(s))&" "," "&INDEX(UPPER(list),1)&" "," "),IF(N=1,TRIM(PROPER(sNew)),DropPrepositions(sNew,DROP(list,,1)))))

But you need to be careful to get your list in the right order. Here's a wrong way:

ABCD
1dede losde ladel
2
3GabrielaLópez-Morenodel PinoGabriela López-Moreno Pino
4Carlosde la TorreRamírezCarlos La Torre Ramírez
5Juan AntonioGarcíaGarcíaJuan Antonio García García
6María de los ÁngelesOjedade MorataMaría Los Ángeles Ojeda Morata
7AAA de BBBCCC de la DDDde EEEEAaa Bbb Ccc La Ddd Eeee
8
Sheet1
Cell Formulas
RangeFormula
D3:D7D3=TEXTJOIN(" ",,DropPrepositions(A3:C3,A$1:D$1))
I'm going to test this thoroughly. Thanks for your help! Give me a few hours.
 
Upvote 0
See if this is it.

RonOliver_1.xlsm
ABCDEFK
1JorgeGuzmánPedrazaJorge Guzmán Pedraza
2DavidGarcíaGarcíaDavid García GarcíaDeletede los
3TamaraBasMorenoTamara Bas Morenode las
4MiguelUreñaPérezMiguel Ureña Pérezde la
5MónicaEstadellasVacaMónica Estadellas Vacadel
6LenaViejoAlegreLena Viejo Alegrede
7FrancescoSchettinoFrancesco Schettinoel
8MartaArenasCabrerizoMarta Arenas Cabrerizola
9AuraLópezAlcázarAura López Alcázarlos
10JavierAlarcónAznarJavier Alarcón Aznarlas
11NabilHrairaNabil Hrairay
12JavierFernándezParraJavier Fernández Parrai
13FátimaAndradeBayarriFátima Andrade Bayarridels
14NoemiCaravantesFenollosaNoemi Caravantes Fenollosade les
15GabrielaLópez-Morenodel PinoGabriela López-Moreno Pinoles
16Carlosde la TorreRamírezCarlos Torre Ramírezels
17Juan AntonioGarcíaGarcíaJuan Antonio García GarcíaDe Los
18María de los ÁngelesOjedade MorataMaría Ángeles Ojeda MorataDe Las
19De La
20Del
21De
22El
23La
24Los
25Las
26Y
27I
28Dels
29De Les
30Les
31Els
Sheet2
Cell Formulas
RangeFormula
D1:D18D1=LET(nm,TEXTJOIN(" ",1,A1:C1),n,REDUCE(nm,K$2:K$31,LAMBDA(s,r,SUBSTITUTE(s," "&r&" "," "))),IF(F$2="Delete",n,nm))
 
Upvote 0
Solution
See if this is it.

RonOliver_1.xlsm
ABCDEFK
1JorgeGuzmánPedrazaJorge Guzmán Pedraza
2DavidGarcíaGarcíaDavid García GarcíaDeletede los
3TamaraBasMorenoTamara Bas Morenode las
4MiguelUreñaPérezMiguel Ureña Pérezde la
5MónicaEstadellasVacaMónica Estadellas Vacadel
6LenaViejoAlegreLena Viejo Alegrede
7FrancescoSchettinoFrancesco Schettinoel
8MartaArenasCabrerizoMarta Arenas Cabrerizola
9AuraLópezAlcázarAura López Alcázarlos
10JavierAlarcónAznarJavier Alarcón Aznarlas
11NabilHrairaNabil Hrairay
12JavierFernándezParraJavier Fernández Parrai
13FátimaAndradeBayarriFátima Andrade Bayarridels
14NoemiCaravantesFenollosaNoemi Caravantes Fenollosade les
15GabrielaLópez-Morenodel PinoGabriela López-Moreno Pinoles
16Carlosde la TorreRamírezCarlos Torre Ramírezels
17Juan AntonioGarcíaGarcíaJuan Antonio García GarcíaDe Los
18María de los ÁngelesOjedade MorataMaría Ángeles Ojeda MorataDe Las
19De La
20Del
21De
22El
23La
24Los
25Las
26Y
27I
28Dels
29De Les
30Les
31Els
Sheet2
Cell Formulas
RangeFormula
D1:D18D1=LET(nm,TEXTJOIN(" ",1,A1:C1),n,REDUCE(nm,K$2:K$31,LAMBDA(s,r,SUBSTITUTE(s," "&r&" "," "))),IF(F$2="Delete",n,nm))
I clearly did something wrong, and I still don't know what it was. Thanks so much for helping me out!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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