Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I combine multiple nested Substitute functions in Excel?

Tags:

I am trying to set up a function to reformat a string that will later be concatenated. An example string would look like this:

Standard_H2_W1_Launch_123x456_S_40K_AB 

Though sometimes the "S" doesn't exist, and sometimes the "40K" is "60K" or not there, and the "_AB" can also be "_CD" or _"EF". Finally, all underscores need to be changed to hyphens. The final product should look like this:

Standard-H2-W1-Launch-123x456- 

I have four functions that if ran one after the other will take care of all of this:

=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2,"_AB","_"),"_CD","_"),"_EF","_")  =SUBSTITUTE(SUBSTITUTE(B2,"_40K",""),"_60K","")  =SUBSTITUTE(C2,"_S_","_")  =SUBSTITUTE(D2,"_","-") 

I've tried a number of ways of combining these into one function, but I'm relatively new to this level of excel so I'm at a loss. Is there anyway to combine all of this so that it executes one command after the other in one cell?

like image 434
samanthathyme Avatar asked Mar 11 '14 00:03

samanthathyme


People also ask

Can you use multiple substitute function in Excel?

As is the case with the Excel REPLACE function, you can nest several SUBSTITUTE functions within a single formula to do several substitutions at a time, i.e. substitute several characters or substrings with a single formula.

Can you combine multiple functions in Excel?

The basic concept that you need to understand is that you can put multiple functions within a single cell, inside of other functions (or next to other functions using concatenation - though concatenation is beyond the scope of this article).


2 Answers

To simply combine them you can place them all together like this:

=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2,"_AB","_"),"_CD","_"),"_EF","_"),"_40K",""),"_60K",""),"_S_","_"),"_","-") 

(note that this may pass the older Excel limit of 7 nested statements. I'm testing in Excel 2010


Another way to do it is by utilizing Left and Right functions.

This assumes that the changing data on the end is always present and is 8 characters long

=SUBSTITUTE(LEFT(A2,LEN(A2)-8),"_","-") 

This will achieve the same resulting string


If the string doesn't always end with 8 characters that you want to strip off you can search for the "_S" and get the current location. Try this:

=SUBSTITUTE(LEFT(A2,FIND("_S",A2,1)),"_","-") 
like image 139
Automate This Avatar answered Sep 17 '22 15:09

Automate This


Thanks for the idea of breaking down a formula Werner!

Using Alt+Enter allows one to put each bit of a complex substitute formula on separate lines: they become easier to follow and automatically line themselves up when Enter is pressed.

Just make sure you have enough end statements to match the number of substitute( lines either side of the cell reference.

As in this example:

= substitute( substitute( substitute( substitute( B11 ,"(","") ,")","") ,"[","") ,"]","") 

becomes:

= SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE(B12,"(",""),")",""),"[",""),"]","") 

which works fine as is, but one can always delete the extra paragraphs manually:

=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(B12,"(",""),")",""),"[",""),"]","") 

Name > substitute()

[American Samoa] > American Samoa

like image 40
Phil Avatar answered Sep 21 '22 15:09

Phil