0

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

Jesse
  • 727
  • 13
  • 44

3 Answers3

4

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:

enter image description here

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

$2x$1

Created with RegexBuddy

Ron Rosenfeld
  • 53,870
  • 7
  • 28
  • 60
2

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.

Here is a little example: enter image description here

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))
B 7
  • 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
2

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!

Brian
  • 2,078
  • 1
  • 15
  • 28
  • note that in the switch = ... the x must be between " ". – Jesse Aug 11 '16 at 10:46
  • @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