您好我有一个包含字符串(产品名称)的列。 现在这些格式为名称LenghtxWidth ,例如绿色框20x30。现在我需要在这个例子中用30改变20,所以我得到绿盒30x20,任何想法我怎么能实现这个? 感谢
答案 0 :(得分:4)
以下是公式解决方案,以及使用正则表达式的VBA解决方案:
<强>式强>
=LEFT(A1,FIND(TRIM(RIGHT(SUBSTITUTE(A1," ",REPT(" ",99)),99)),A1)-1)&
MID(TRIM(RIGHT(SUBSTITUTE(A1," ",REPT(" ",99)),99)),SEARCH("x",TRIM(RIGHT(SUBSTITUTE(A1," ",REPT(" ",99)),99)))+1,99)&
"x"&
LEFT(TRIM(RIGHT(SUBSTITUTE(A1," ",REPT(" ",99)),99)),SEARCH("x",TRIM(RIGHT(SUBSTITUTE(A1," ",REPT(" ",99)),99)))-1)
<强> UDF 强>
Option Explicit
Function RevWL(S As String)
Dim RE As Object
Const sPat As String = "(\d+.?\d*)x(\d+.?\d*)"
'If L or W might start with a decimal point, and not a digit,
'Then change sPat to: (\d*.?\d+)x(\d*.?\d+)
Set RE = CreateObject("vbscript.regexp")
With RE
.Global = True
.ignorecase = True
.Pattern = sPat
RevWL = .Replace(S, "$2x$1")
End With
End Function
以下是我测试过的各类数据的示例:
通过查看最后一个以空格分隔的子字符串LxW
,然后在x
之后和之前反转该部分,然后将所有内容连接在一起,该公式工作。
正则表达式模式捕获两个数字(可以是整数或小数,只要以整数开头 - 尽管可以根据需要更改),然后将它们反转。
以下是正则表达式(和替换字符串)的更详细说明,其中包含指向教程的链接:
(\d+.?\d*)x(\d+.?\d*)
选项:不区分大小写; ^ $不匹配的换行
(\d+.?\d*)
x
(\d+.?\d*)
2×$ $ 1
创建答案 1 :(得分:2)
好的,所以使用VBA真的很容易,但是如果你只想要一些公式,你可以使用一些列来分割文本,然后连接你的单元格。
当然B1-4是可选的。这里只有更具可读性的东西,但你只能使用一个公式
=CONCATENATE(LEFT(A1, SEARCH(" ",A1,1)-1)," ",RIGHT(RIGHT(A1,LEN(A1)-SEARCH(" ",A1,1)),LEN(RIGHT(A1,LEN(A1)-SEARCH(" ",A1,1)))-SEARCH("x",RIGHT(A1,LEN(A1)-SEARCH(" ",A1,1)),1)),"x",LEFT(RIGHT(A1,LEN(A1)-SEARCH(" ",A1,1)), SEARCH("x",RIGHT(A1,LEN(A1)-SEARCH(" ",A1,1)),1)-1))
如果您的名字中有多个空格,则可以使用此公式搜索文本中的最后一个空间
=CONCATENATE(LEFT(A1, SEARCH("^^",SUBSTITUTE(A1," ","^^",LEN(A1)-LEN(SUBSTITUTE(A1," ",""))))-1)," ",RIGHT(RIGHT(A1,LEN(A1)-SEARCH("^^",SUBSTITUTE(A1," ","^^",LEN(A1)-LEN(SUBSTITUTE(A1," ",""))))),LEN(RIGHT(A1,LEN(A1)-SEARCH("^^",SUBSTITUTE(A1," ","^^",LEN(A1)-LEN(SUBSTITUTE(A1," ",""))))))-SEARCH("x",RIGHT(A1,LEN(A1)-SEARCH("^^",SUBSTITUTE(A1," ","^^",LEN(A1)-LEN(SUBSTITUTE(A1," ",""))))),1)),"x",LEFT(RIGHT(A1,LEN(A1)-SEARCH("^^",SUBSTITUTE(A1," ","^^",LEN(A1)-LEN(SUBSTITUTE(A1," ",""))))), SEARCH("x",RIGHT(A1,LEN(A1)-SEARCH("^^",SUBSTITUTE(A1," ","^^",LEN(A1)-LEN(SUBSTITUTE(A1," ",""))))),1)-1))
答案 2 :(得分:2)
以下是适用于您的VBA解决方案:
Option Explicit
Function Switch(r As Range) As String
Dim measurement As String
Dim firstPart As String
Dim secondPart As String
measurement = Right(r, Len(r) - InStrRev(r, " "))
secondPart = Right(measurement, Len(measurement) - InStr(1, measurement, "x"))
firstPart = Left(measurement, InStr(1, measurement, "x") - 1)
Switch = Left(r, InStrRev(r, " ") - 1) & " " & secondPart & "x" & firstPart
End Function
您可以将其粘贴到VBE (Visual Basic Editor)
的常规模块中,并将其用作常规函数/公式。如果您的值在单元格A1中,则在单元格B1中键入=Switch(A1)
。希望它有所帮助!