我正在尝试设置一个函数来重新格式化一个稍后将被连接的字符串。示例字符串如下所示:
Standard_H2_W1_Launch_123x456_S_40K_AB
虽然有时“ S ”不存在,有时“40K”是“60K”或不存在,“_ AB”也可以是“_CD”或_“EF ”。最后,所有下划线都需要更改为连字符。最终产品应如下所示:
Standard-H2-W1-Launch-123x456-
我有四个功能,如果一个接一个地运行将负责所有这些:
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2,"_AB","_"),"_CD","_"),"_EF","_")
=SUBSTITUTE(SUBSTITUTE(B2,"_40K",""),"_60K","")
=SUBSTITUTE(C2,"_S_","_")
=SUBSTITUTE(D2,"_","-")
我已经尝试了将这些功能组合成一个功能的多种方法,但我对这个级别的excel相对较新,所以我很茫然。无论如何要将所有这些组合起来,以便它在一个单元格中执行另一个命令吗?
答案 0 :(得分:17)
要简单地将它们组合起来,您可以将它们全部放在一起:
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2,"_AB","_"),"_CD","_"),"_EF","_"),"_40K",""),"_60K",""),"_S_","_"),"_","-")
(请注意,这可能会超过7个嵌套语句的旧Excel限制。我在Excel 2010中进行测试
另一种方法是使用Left
和Right
函数。
这假设末尾的数据变化始终存在且长度为8个字符
=SUBSTITUTE(LEFT(A2,LEN(A2)-8),"_","-")
这将实现相同的结果字符串
如果字符串并不总是以您要剥离的8个字符结尾,则可以搜索“_S”并获取当前位置。试试这个:
=SUBSTITUTE(LEFT(A2,FIND("_S",A2,1)),"_","-")
答案 1 :(得分:2)
感谢分解公式Werner的想法!
使用Alt + Enter可以将复杂替代公式的每一位放在不同的行上:它们变得更容易跟随并在按下Enter时自动排列。
请确保您有足够的结束语句以匹配单元格引用两侧的substitute(
行数。
如下例所示:
=
substitute(
substitute(
substitute(
substitute(
B11
,"(","")
,")","")
,"[","")
,"]","")
<强>变为:强>
=
SUBSTITUTE(
SUBSTITUTE(
SUBSTITUTE(
SUBSTITUTE(B12,"(",""),")",""),"[",""),"]","")
可以正常工作,但可以随时手动删除额外的段落:
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(B12,"(",""),")",""),"[",""),"]","")
名称&gt;替代()强>
[美属萨摩亚]&gt;美属萨摩亚
答案 2 :(得分:1)
我会使用以下方法:
=SUBSTITUTE(LEFT(A2,LEN(A2)-X),"_","-")
其中X
表示您不会追求的事物的长度。而且,对于X
,我会使用
(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)
如果找不到ISERROR(FIND("X",.,.))*x
,则上述X
将返回0,如果找到,则x
(X
的长度)将返回0。所以从技术上讲,你可以从右侧修剪A2
并进行匹配。
这种方法优于其他提到的方法是,更明显的是替换(或删除)正在发生,因为“替换”不是嵌套的。
答案 3 :(得分:0)
=SUBSTITUTE(text, old_text, new_text)
if: a=!, b=@, c=#,... x=>, y=?, z=~, " "=" "
then: abcdefghijklmnopqrstuvwxyz ... try this out
equals: !@#$%^&*()-=+[]\{}|;:/<>?~ ... ;}? ;*(| ]:;
(1)替换文本在单元格A1中
(2)最多64个替换级别(下面的公式只有27个级别[字母+空格])
(2)“ old_text”也不能是“ new_text”(即:如果a = z。:z不能是“ 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)
公式为:
=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","~")," "," ")