Hello I have a column with strings (names of products) in it. Now these are formatted as Name LenghtxWidth, example Green box 20x30. Now I need to change the 20 with the 30 in this example so I get Green box 30x20, any ideas how I can achieve this? Thanks
-
You would like to do that without using VBA? – B 7 Aug 09 '16 at 11:31
-
Yes, or excel formulas, as long as I get the result I'm looking for – Jesse Aug 09 '16 at 11:38
-
@Jesse are the length and width always 2 digits? So no "100x50" or "5x10"? If so, then this could be done easily with formulas – elmer007 Aug 09 '16 at 12:09
3 Answers
Here is both a formula solution, as well as a VBA solution using Regular Expressions:
Formula
=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
Here is an example of the kinds of data I tested with:
The Formula works by looking at the last space-separated substring which would be LxW
, then reversing the portion after and before the x
, then concatenating everything back together.
The regex pattern captures the two numbers (could be integers or decimals, so long as the start with an integer -- although that could be changed if needed), and reversing them.
Here is a more detailed explanation of the regex (and the replacement string) with links to a tutorial:
(\d+.?\d*)x(\d+.?\d*)
(\d+.?\d*)x(\d+.?\d*)
Options: Case insensitive; ^$ don’t match at line breaks
- Match the regex below and capture its match into backreference number 1
(\d+.?\d*)
- Match the character “x” literally
x
- Match the regex below and capture its match into backreference number 2
(\d+.?\d*)
$2x$1
- Insert the text that was last matched by capturing group number 2
$2
- Insert the character “x” literally
x
- Insert the text that was last matched by capturing group number 1
$1
Created with RegexBuddy

- 53,870
- 7
- 28
- 60
-
I don't understand your UDF but that is really cool and a great solution. I need to learn that. – Brian Aug 09 '16 at 12:23
-
-
-
@RonRosenfeld Thank you for that explanation, Ron! I stumbled upon this [site](http://www2.hawaii.edu/~chenx/reading/VBA/vb_regexp.html). Have you seen this? – Brian Aug 10 '16 at 11:45
Ok, so it is really easier to use VBA, but if you want only some formulas you can use some columns to split your text and then concatenate your cells.
Of course B1-4 are optional. It is here only to have something more readable, but you can do use only one formula
=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))
If you have several spaces in your names, you can use this formula that will search the last space in the text
=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))

- 670
- 7
- 23
-
thanks for your answer, can u maybe provide an example with the vba script? Since the above formulas doens't work when there are multiple spaces in the name, example Yellow Small Case 30x15 => Small Case 30x15 – Jesse Aug 09 '16 at 12:05
-
You can use this formula to get a name with several spaces `=LEFT(A1, SEARCH("^^",SUBSTITUTE(A1," ","^^",LEN(A1)-LEN(SUBSTITUTE(A1," ",""))))-1)` – B 7 Aug 09 '16 at 12:26
Here is a VBA solution that will work for you:
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
You can paste this in a regular module in the VBE (Visual Basic Editor)
and use it as a regular function/formula. If your value is in cell A1 then type =Switch(A1)
in cell B1. Hope it helps!

- 2,078
- 1
- 15
- 28
-
-
@Jesse Good eye! Thanks for bringing that to my attention! You know what happened was I originally had an `x` variable to find the `x` in `20x30' and realized I didn't need it. When I deleted the variable I failed to make `x` in the switch statement a string literal. Thanks again! :-) – Brian Aug 11 '16 at 10:59
-
Yes I thought so, thanks for the answer this is what got me the solution to my problem in the end! – Jesse Aug 11 '16 at 11:11
-
Not sure if you switched the hardcoded `Range("A1")` portions to the `r` variable - FYI. – Brian Aug 11 '16 at 11:32