AREPLACE

=AREPLACE(ar,a,b)

ar
array
a
1D, column array, values to be replaced (only column, if in a row please transpose)
b
1D, column array, replacement values (only column, if in a row please transpose)

AREPLACE, array replace, replaces all occurrences of "a" found in "ar" with values of "b".

Xlambda

Well-known Member
Joined
Mar 8, 2021
Messages
832
Office Version
  1. 365
Platform
  1. Windows
AREPLACE !! recursive!! (array replace), replaces all occurrences of "a" found in "ar" with values of "b"
Excel Formula:
=LAMBDA(ar,a,b,
    LET(n,ROWS(a),
       x,INDEX(a,n),y,INDEX(b,n),
       IF(n=1,SUBSTITUTE(ar,x,y),AREPLACE(SUBSTITUTE(ar,x,y),INDEX(a,SEQUENCE(n-1)),INDEX(b,SEQUENCE(n-1))))
   )
)
LAMBDA 6.0.xlsx
ABCDEFGHIJKLMNOPQRSTU
1SampleLambda that appends string values of an array with its PROPER values.
2=IF(SEQUENCE(7),"Aaa aaa Bbb bbb Ccc ccc Ddd ddd")T_PROPER(a)=LAMBDA(a,LET(x,IF(ROWS(a)=1,TRANSPOSE(a),a),p,PROPER(a),n,ROWS(x),s,SEQUENCE(2*n)-n,IF(s<=0,x,INDEX(p,s))))
3Aaa aaa Bbb bbb Ccc ccc Ddd ddd
4Aaa aaa Bbb bbb Ccc ccc Ddd dddreplaceaaabbbcccddd
5Aaa aaa Bbb bbb Ccc ccc Ddd dddwithxxyyzzww
6Aaa aaa Bbb bbb Ccc ccc Ddd ddd
7Aaa aaa Bbb bbb Ccc ccc Ddd dddI12=T_PROPER(J4:M4)
8Aaa aaa Bbb bbb Ccc ccc Ddd dddJ12=T_PROPER(J5:M5)
9Aaa aaa Bbb bbb Ccc ccc Ddd ddd
10=AREPLACE(A3#,T_PROPER(C14:C17),T_PROPER(D14:D17))
11=AREPLACE(A3#,C14:C17,D14:D17)Xx xx Yy yy Zz zz Ww wwrpl Pwith P
12Aaa xx Bbb yy Ccc zz Ddd wwXx xx Yy yy Zz zz Ww wwaaaxx
13Aaa xx Bbb yy Ccc zz Ddd wwreplacewithXx xx Yy yy Zz zz Ww wwbbbyy
14Aaa xx Bbb yy Ccc zz Ddd wwaaaxxXx xx Yy yy Zz zz Ww wwccczz
15Aaa xx Bbb yy Ccc zz Ddd wwbbbyyXx xx Yy yy Zz zz Ww wwdddww
16Aaa xx Bbb yy Ccc zz Ddd wwccczzXx xx Yy yy Zz zz Ww wwAaaXx
17Aaa xx Bbb yy Ccc zz Ddd wwdddwwXx xx Yy yy Zz zz Ww wwBbbYy
18Aaa xx Bbb yy Ccc zz Ddd wwCccZz
19DddWw
20=AREPLACE(A3#,AFLATTEN(C23:D24),E23:E26)
21Aaa xx Bbb yy Ccc zz Ddd ww
22Aaa xx Bbb yy Ccc zz Ddd wwreplacewith
23Aaa xx Bbb yy Ccc zz Ddd wwaaabbbxx
24Aaa xx Bbb yy Ccc zz Ddd wwcccdddyy
25Aaa xx Bbb yy Ccc zz Ddd wwzz
26Aaa xx Bbb yy Ccc zz Ddd wwww
27Aaa xx Bbb yy Ccc zz Ddd ww
28
AREPLACE post
Cell Formulas
RangeFormula
A2,A20,G10,A11A2=FORMULATEXT(A3)
A3:A9A3=IF(SEQUENCE(7),"Aaa aaa Bbb bbb Ccc ccc Ddd ddd")
I7I7=FORMULATEXT(I12)
I8I8=FORMULATEXT(J12)
G11:G17G11=AREPLACE(A3#,T_PROPER(C14:C17),T_PROPER(D14:D17))
A12:A18A12=AREPLACE(A3#,C14:C17,D14:D17)
I12:I19I12=T_PROPER(J4:M4)
J12:J19J12=T_PROPER(J5:M5)
A21:A27A21=AREPLACE(A3#,AFLATTEN(C23:D24),E23:E26)
Dynamic array formulas.
 
Upvote 0
Note: The following Lambda function is used in the sample mini-sheet above.
AFLATTEN

T_PROPER Lambda function is provided in Cell F2.
 
AREPLACE(ar,a,[b ],[cs],[in])
Brand new function, 4 in 1, replace with an array, replace with delimiter, remove, insert.
Same name, more arguments, added functionality, non recursive. NEW!! 2d REDUCE. Calls AFLAT.
REDUCE uses as "initial value" argument a 2D array.
Replaces all occurrences of words of array "a" found in "ar" with their correspondent values of array "b"
ar: array
a: array
[b ]: array

new arguments:
[cs]: case sensitive argument
[in]: insert argument

Excel Formula:
=LAMBDA(ar,a,[b],[cs],[in],
     LET(x,AFLAT(a),f,AFLAT(b,1),y,IF(in,x&f,f),r,SEQUENCE(ROWS(x)),xl,LOWER(x),xc,UPPER(x),xp,PROPER(x),
        xx,IF(in,x,IFS(cs=0,x,cs=2,CHOOSE({1,2,3},x,xl,xc),OR(cs=-1,cs=1,cs=3),CHOOSE({1,2},x,SWITCH(cs,-1,xl,1,xc,3,xp)))),c,SEQUENCE(COLUMNS(xx)),
        z,REDUCE(ar,c,LAMBDA(a,i,REDUCE(a,r,LAMBDA(a,j,SUBSTITUTE(a,INDEX(xx,j,i),IFERROR(INDEX(y,IF(ROWS(y)=1,1,j)),"")))))),
        IF(AND(ISNA(xx)),"check cs",IFERROR(--z,z))
    )
)
LAMBDA 1.1.2.xlsx
ABCDEFGHIJK
1new!! AREPLACE functionality using 2D REDUCE, 4 in 1 function, replace,replace with delimiter,remove,insert
2Replaces all occurrences of words of array "a" found in "ar" with their correspondent values of array "b"
3If AREMOVE and AKEEP refers to chars, this function refers to whole words
4[cs]: case sensitive argument
5[in]: insert argument
6Part 1: how correspondent arrays a,b work, when "cs" and "in" arguments omitted
7
8ar,samplea
9rd45<red>,<cyan>cy34<blue>le32,ya66<cyan>red
10een27<green>,bl87<blue>dr56<red>,ne74<green>green
11blue
12b,omitted, remove words functionalitycyan
13=AREPLACE(A9:B10,D9:D12)
14rd45<>,<>cy34<>le32,ya66<>
15een27<>,bl87<>dr56<>,ne74<>
16
17b,"-|-",one element, replace with a delimiter functionality
18=AREPLACE(A9:B10,D9:D12,"-|-")b
19rd45<-|->,<-|->cy34<-|->le32,ya66<-|->-|-
20een27<-|->,bl87<-|->dr56<-|->,ne74<-|->
21b
22b, array , same nr. of elements with "a"clr1
23=AREPLACE(A9:B10,D9:D12,D22:D25)clr2
24rd45<clr1>,<clr4>cy34<clr3>le32,ya66<clr4>clr3
25een27<clr2>,bl87<clr3>dr56<clr1>,ne74<clr2>clr4
26
27b1,array , more than 1 element but less than "a" elements
28=AREPLACE(A9:B10,D9:D12,D29:D30)b1b2b3
29rd45<clr1>,<>cy34<>le32,ya66<>clr1clr1clr1
30een27<clr2>,bl87<>dr56<clr1>,ne74<clr2>clr2clr2
31clr3clr3
32b2 array with "gaps"clr4clr4
33=AREPLACE(A9:B10,D9:D12,F29:F32)clr5
34rd45<clr1>,<clr4>cy34<clr3>le32,ya66<clr4>
35een27<>,bl87<clr3>dr56<clr1>,ne74<>
36
37b3,array, more elements than "a"
38=AREPLACE(A9:B10,D9:D12,H29:H33)
39rd45<clr1>,<clr4>cy34<clr3>le32,ya66<clr4>
40een27<clr2>,bl87<clr3>dr56<clr1>,ne74<clr2>
41replace functionality
42If rows(b)<rows(a), non correspondent values of "a" are replaced with null strings
43If rows(b)>rows(a), extra values of "b" have no effect
44
new AREPLACE post 1
Cell Formulas
RangeFormula
A13,A38,A33,A28,A23,A18A13=FORMULATEXT(A14)
A14:B15A14=AREPLACE(A9:B10,D9:D12)
A19:B20A19=AREPLACE(A9:B10,D9:D12,"-|-")
A24:B25A24=AREPLACE(A9:B10,D9:D12,D22:D25)
A29:B30A29=AREPLACE(A9:B10,D9:D12,D29:D30)
A34:B35A34=AREPLACE(A9:B10,D9:D12,F29:F32)
A39:B40A39=AREPLACE(A9:B10,D9:D12,H29:H33)
Dynamic array formulas.
 
LAMBDA 1.1.2.xlsx
ABCDEFGHIJKLMNOPQ
1Part 2: "cs" case sensitive argumend functionality.
2Since the core of the function is the SUBSTITUTE function, the function is case sensitive also.
3What function does, expands the "a" array with its correspondent values acording to "cs" values
4cs: 0 or omitted will leave the "a" array as it is
5cs: -1, expands "a" with an extra column of lower(a)
6cs: 1 expands "a" with an extra column of upper(a)
7cs: 2, expands "a" with 2 columns lower(a)+upper(a)
8cs: 3, expands "a" with an extra column of proper(a)
9new "a" calculated inside the function for different "cs" values
10acs,0cs,-1cs,1cs,2cs,3
11xYzxYzxYzxyzxYzXYZxYzxyzXYZxYzXyz
12aBcaBcaBcabcaBcABCaBcabcABCaBcAbc
13
14The nested or 2D REDUCE part of the function, is able to handle the new"a" 2 dimensions whenever the case
15[ z,REDUCE(ar,c,LAMBDA(a,i,REDUCE(a,r,LAMBDA(a,j,SUBSTITUTE(a,INDEX(xx,j,i),IFERROR(INDEX(y,IF(ROWS(y)=1,1,j)),"")))))) ]
16To acomplish same functionality before REDUCE, only nested recursion for nr of rows/clms could have done it.
17
18Previous AREPLACE function covered only PROPER versatility with a separate function,
19new AREPLACE has it embedded in the function itself, plus lower/upper case versatility
20
21sampleab
22xYz,xyzxYz,XYZxYz,XyzxYz(1)
23aBc,abcaBc,ABCaBc,AbcaBc(2)
24
25cs,omittedcs,-1cs,1
26=AREPLACE(A22:C23,E22:E23,G22:G23)=AREPLACE(A22:C23,E22:E23,G22:G23,-1)=AREPLACE(A22:C23,E22:E23,G22:G23,1)
27(1),xyz(1),XYZ(1),Xyz(1),(1)(1),XYZ(1),Xyz(1),xyz(1),(1)(1),Xyz
28(2),abc(2),ABC(2),Abc(2),(2)(2),ABC(2),Abc(2),abc(2),(2)(2),Abc
29
30cs,wrong argument (-2)cs,2cs,3
31=AREPLACE(A22:C23,E22:E23,G22:G23,-2)=AREPLACE(A22:C23,E22:E23,G22:G23,2)=AREPLACE(A22:C23,E22:E23,G22:G23,3)
32check cs(1),(1)(1),(1)(1),Xyz(1),xyz(1),XYZ(1),(1)
33(2),(2)(2),(2)(2),Abc(2),abc(2),ABC(2),(2)
34
new AREPLACE post 2
Cell Formulas
RangeFormula
F11:F12,L11:L12F11=LOWER(E11:E12)
I11:I12I11=UPPER(H11:H12)
M11:M12M11=UPPER(K11:K12)
P11:P12P11=PROPER(O11:O12)
A26,E26,I26,A31,E31,I31A26=FORMULATEXT(A27)
A27:C28A27=AREPLACE(A22:C23,E22:E23,G22:G23)
E27:G28E27=AREPLACE(A22:C23,E22:E23,G22:G23,-1)
I27:K28I27=AREPLACE(A22:C23,E22:E23,G22:G23,1)
A32A32=AREPLACE(A22:C23,E22:E23,G22:G23,-2)
E32:G33E32=AREPLACE(A22:C23,E22:E23,G22:G23,2)
I32:K33I32=AREPLACE(A22:C23,E22:E23,G22:G23,3)
Dynamic array formulas.
 
LAMBDA 1.1.2.xlsx
ABCDEFGHIJKLMNOPQRSTUV
1Part 3: "in", insert argument functionality.How function calculates "new b" when in=1, 3 different scenarios
2in: 0 or omitted, array "b" not altered, replace functionalityab1new b1b2new b2b3new b3
3in: 1 or <>0, new"b"=a&b, insert functionalityred/red//1red/1/red/
4Note: If in<>0, cs will take 0 value, has no effectgreengreen//2green/2\green\
5Inserting is posible only for "a" values as they are.blueblue//3blue/3blue
6cyancyan//4cyan/4|cyan|
7ar,sample 1
8abredAB,green27cs,2,in,omitted, no inserting
9red/blueXY,cyan34abar,sample 2=AREPLACE(L10:L11,H10:H13,J10:J13,2)
10greenred/1,redAB,GREEN27/1,AB,/2,27
11blue=AREPLACE(E8:E9,H3:H6,J3,,1)green/2,BLUEXY,cyan34/3,XY,/4,34
12cyanred/AB,green/27blue/3,
13blue/XY,cyan/34cyan/4,cs,2,in,1 ,inserting takes place
14/1,=AREPLACE(L10:L11,H10:H13,J10:J13,2,1)
15/2,=AREPLACE(E8:E9,A9:A12,C14:C17,,1)red/1,AB,GREEN27
16/3,red/1,AB,green/2,27BLUEXY,cyan/4,34
17/4,blue/3,XY,cyan/4,34Because in=1, cs value has no effect, the formula considers it as a 0
18
19/1,
20=AREPLACE(E8:E9,A9:A12,C19:C22,,1)
21red/1,AB,green27
22/4,blueXY,cyan/4,34
23
new AREPLACE post 3
Cell Formulas
RangeFormula
L3:L6L3=H3:H6&J3
P3:P6P3=H3:H6&N3:N6
T3:T6T3=IFNA(H3:H6&R3:R6,"")
P9,E20,E15,P14,E11P9=FORMULATEXT(P10)
P10:P11P10=AREPLACE(L10:L11,H10:H13,J10:J13,2)
E12:E13E12=AREPLACE(E8:E9,H3:H6,J3,,1)
P15:P16P15=AREPLACE(L10:L11,H10:H13,J10:J13,2,1)
E16:E17E16=AREPLACE(E8:E9,A9:A12,C14:C17,,1)
E21:E22E21=AREPLACE(E8:E9,A9:A12,C19:C22,,1)
Dynamic array formulas.
 

Forum statistics

Threads
1,216,073
Messages
6,128,645
Members
449,461
Latest member
kokoanutt

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