Replace All strings starting with a number ending in 'cm'

alex0182828

Board Regular
Joined
Jun 20, 2012
Messages
88
Office Version
  1. 365
Platform
  1. MacOS
I need remove string starting in a number and ending in 'cm' or 'CM' and replace with a # symbol.

Number can be anything and the string between the first number and the cm can be or variable length.

There will only be one occurance of this pattern in each cell.

Made example table can anyone help ? Thanks

Book8
AB
1Source Result
2Frog *** Halskette 34,5 - 56cmFrog *** Halskette #
3Rund Pavé - Fassung Nosiw 20,5 - 81cmRund Pavé - Fassung Nosiw #
4adasd 85,5 - 91CM Fuchwanz Ketteadasd # Fuchwanz Kette
5Rosoldet CAAAsf 35,5 - 71 cm Fuchsschwanz KetteRosoldet CAAAsf # Fuchsschwanz Kette
Sheet1
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Try this:
Excel Formula:
=REPLACE(A2,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A2&"0123456789")),FIND("cm",LOWER(A2))-MIN(FIND({0,1,2,3,4,5,6,7,8,9},A2&"0123456789"))+2,"#")
 
Upvote 0
Thanks works perfect for the examples i have but it highlighted i missed out that there can be other numbers in the cell so examples like

8 frog cat 90,5 - 95,5cm -> 8 frog cat #
frog 7 bat 67 - 95cm happy -> frog 7 bat # happy
 
Upvote 0
Willing to do anything says a desperate person.

Im running up to date mac office 365 is thats important to know.

A
 
Upvote 0
Try this:
VBA Code:
Sub alex0182828_1()

Dim i As Long, n As Long
Dim tx As String
Dim va

va = Range("A2", Cells(Rows.Count, "A").End(xlUp))
ReDim vb(1 To UBound(va, 1), 1 To 1)
For i = 1 To UBound(va, 1)
    If InStr(1, va(i, 1), "cm", vbTextCompare) > 0 Then
        tx = Trim((va(i, 1)))
        tx = Replace(tx, "CM", "cm")
            arx = Split(tx, "cm")
            ary = Split(Trim(arx(0)), " ")
        
        n = UBound(ary)
            tx = ary(n - 2) & " - " & ary(n)
                va(i, 1) = Replace(arx(0), tx, " # ") & arx(1)
    End If
Next


Range("B2").Resize(UBound(va, 1), 1) = va
End Sub

It worked on your small sample:
Book1
AB
1Source
2Frog *** Halskette 34,5 - 56cmFrog *** Halskette #
3Rund Pavé - Fassung Nosiw 20,5 - 81cmRund Pavé - Fassung Nosiw #
4adasd 85,5 - 91CM Fuchwanz Ketteadasd # Fuchwanz Kette
5Rosoldet CAAAsf 35,5 - 71 cm Fuchsschwanz KetteRosoldet CAAAsf # Fuchsschwanz Kette
68 frog cat 90,5 - 95,5cm8 frog cat #
7frog 7 bat 67 - 95cm happyfrog 7 bat # happy
8
Sheet2


but it won't work if you have more than one "cm", such as:
pacman frog 7 bat 67 - 95cm happy
 
Upvote 0
How about (if you have the MAP function):
Book1
AB
1Source Result
2Frog *** Halskette 34,5 - 56cmFrog *** Halskette 34,5 - #
3Rund Pavé - Fassung Nosiw 20,5 - 81cmRund Pavé - Fassung Nosiw 20,5 - #
4adasd 85,5 - 91CM Fuchwanz Ketteadasd 85,5 - # Fuchwanz Kette
5Rosoldet CAAAsf 35,5 - 71 cm Fuchsschwanz KetteRosoldet CAAAsf 35,5 - # Fuchsschwanz Kette
68 frog cat 90,5 - 95,5cm8 frog cat 90,5 - #
7frog 7 bat 67 - 95cm happyfrog 7 bat 67 - # happy
Sheet1
Cell Formulas
RangeFormula
B2:B7B2=TEXTJOIN(" ",,MAP(TEXTSPLIT(SUBSTITUTE(A2," cm","cm")," "),LAMBDA(x,IF(RIGHT(x,2)="cm","#",x))))


Same result with BYCOL:
Book1
AB
1Source Result
2Frog *** Halskette 34,5 - 56cmFrog *** Halskette 34,5 - #
3Rund Pavé - Fassung Nosiw 20,5 - 81cmRund Pavé - Fassung Nosiw 20,5 - #
4adasd 85,5 - 91CM Fuchwanz Ketteadasd 85,5 - # Fuchwanz Kette
5Rosoldet CAAAsf 35,5 - 71 cm Fuchsschwanz KetteRosoldet CAAAsf 35,5 - # Fuchsschwanz Kette
68 frog cat 90,5 - 95,5cm8 frog cat 90,5 - #
7frog 7 bat 67 - 95cm happyfrog 7 bat 67 - # happy
Sheet1
Cell Formulas
RangeFormula
B2:B7B2=TEXTJOIN(" ",,BYCOL(TEXTSPLIT(SUBSTITUTE(A2," cm","cm")," "),LAMBDA(x,IF(RIGHT(x,2)="cm","#",x))))
 
Upvote 0
Here is a one cell formula option:
Book1
AB
1Source Result
2Frog *** Halskette 34,5 - 56cmFrog *** Halskette 34,5 - #
3Rund Pavé - Fassung Nosiw 20,5 - 81cmRund Pavé - Fassung Nosiw 20,5 - #
4adasd 85,5 - 91CM Fuchwanz Ketteadasd 85,5 - # Fuchwanz Kette
5Rosoldet CAAAsf 35,5 - 71 cm Fuchsschwanz KetteRosoldet CAAAsf 35,5 - # Fuchsschwanz Kette
68 frog cat 90,5 - 95,5cm8 frog cat 90,5 - #
7frog 7 bat 67 - 95cm happyfrog 7 bat 67 - # happy
Sheet1
Cell Formulas
RangeFormula
B2:B7B2=LET(data, SUBSTITUTE(A2:A7," cm","cm"), c, MAX(BYROW(data,LAMBDA(x,LEN(x)-LEN(SUBSTITUTE(x," ",""))+1))), xml, "<t><s>" & SUBSTITUTE(data," ","</s><s>") & "</s></t>", f,IFERROR(FILTERXML(xml,"//s[" & SEQUENCE(,c) & "]"),""), m,MAP(f,LAMBDA(x,IF(RIGHT(x,2)="cm","#",x))), BYROW(m,LAMBDA(x,TEXTJOIN(" ",,x))))
Dynamic array formulas.
 
Upvote 0
I think maybe there is some confusion about the use of the # symbol in my example. The results in both your solutions @Georgiboy dont match mine / the results should show exactly as in my demo sheet. i.e a # be inserted.

My Soure data in A2 is

Frog *** Halskette 34,5 - 56cm

the result for this should be

Frog *** Halskette #

where as the result in both your was

Frog *** Halskette 34,5 - #
 
Upvote 0

Forum statistics

Threads
1,216,105
Messages
6,128,861
Members
449,472
Latest member
ebc9

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top