How can I combine multiple nested Substitute functions in Excel?

后端 未结 5 1813
Happy的楠姐
Happy的楠姐 2021-01-17 08:35

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_         


        
相关标签:
5条回答
  • 2021-01-17 09:21

    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

    0 讨论(0)
  • 2021-01-17 09:23
    =SUBSTITUTE(text, old_text, new_text)
    
    if: a=!, b=@, c=#,... x=>, y=?, z=~, " "="     "
    then: abcdefghijklmnopqrstuvwxyz ... try this out
    equals: !@#$%^&*()-=+[]\{}|;:/<>?~     ...     ;}?     ;*(|     ]:;
    

    RULES:

    (1) text to substitute is in cell A1
    (2) max 64 substitution levels (the formula below only has 27 levels [alphabet + space])
    (2) "old_text" cannot also be a "new_text" (ie: if a=z .: z cannot be "old text")

    ---so if a=z,b=y,...y=b,z=a, then the result is 
    ---abcdefghijklmnopqrstuvwxyz = zyxwvutsrqponnopqrstuvwxyz (and z changes to a then changes back to z) ... (pattern starts to fail after m=n, n=m... and n becomes n)
    

    The formula is:

    =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,"a","!"),"b","@"),"c","#"),"d","$"),"e","%"),"f","^"),"g","&"),"h","*"),"i","("),"j",")"),"k","-"),"l","="),"m","+"),"n","["),"o","]"),"p","\"),"q","{"),"r","}"),"s","|"),"t",";"),"u",":"),"v","/"),"w","<"),"x",">"),"y","?"),"z","~")," ","     ")
    
    0 讨论(0)
  • 2021-01-17 09:25

    I would use the following approach:

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

    where X denotes the length of things you're not after. And, for X I'd use

    (ISERROR(FIND("_S",A2,1))*2)+
    (ISERROR(FIND("_40K",A2,1))*4)+
    (ISERROR(FIND("_60K",A2,1))*4)+
    (ISERROR(FIND("_AB",A2,1))*3)+
    (ISERROR(FIND("_CD",A2,1))*3)+
    (ISERROR(FIND("_EF",A2,1))*3)
    

    The above ISERROR(FIND("X",.,.))*x will return 0 if X is not found and x (the length of X) if it is found. So technically you're trimming A2 from the right with possible matches.

    The advantage of this approach above the other mentioned is that it's more apparent what substitution (or removal) is taking place, since the "substitution" is not nested.

    0 讨论(0)
  • 2021-01-17 09:26
    • nesting SUBSTITUTE() in a string can be nasty, however, it's always possible to arrange it:

    0 讨论(0)
  • 2021-01-17 09:27

    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)),"_","-")
    
    0 讨论(0)
提交回复
热议问题