How can I combine multiple nested Substitute functions in Excel?

后端 未结 5 1854
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: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.

提交回复
热议问题