Syntax for 'Any Number'. Replace with "/". VBA

killpaddy

Board Regular
Joined
Jul 31, 2012
Messages
52
Hi,

I have a list of data with a mix of text, numbers and symbols in excel, such as:
'Quick Turn 15, 18, 20, 200
Dual Turn 20
8 Station, 8 Position
QT Nexus 150/200/250/300/350
SQT 200, 250
12 Station, 12 Position
QT Smart 150S / 300 / 350'

(note this is an example of what is contained in a single cell, not 7 consecutive cells)

I would like the ", " to be replaced with "/" between the numbers on the first line (e.g. '15/18/20/200')

however i dont want the ", " after 'station, ' to be replaced

I have this at the moment, but is there a shorter way, possibly using an 'any number' symbol.

Code:
Columns(1).Replace "0, ", "0/"
Columns(1).Replace "1, ", "1/"
Columns(1).Replace "2, ", "2/"
Columns(1).Replace "3, ", "3/"
Columns(1).Replace "4, ", "4/"
Columns(1).Replace "5, ", "5/"
Columns(1).Replace "6, ", "6/"
Columns(1).Replace "7, ", "7/"
Columns(1).Replace "8, ", "8/"
Columns(1).Replace "9, ", "9/"
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
How about this?

You can try =ReplaceComma(A1,False) to replace any letter.

Code:
Function ReplaceComma(sInp As String, Optional RepNum As Boolean = True) As String
    Dim Temp As String
    Dim Pattern As String
    
    ReplaceComma = sInp
    
    If RepNum Then
        Pattern = "((\d+),)+"
    Else
        Pattern = "([A-Za-z]+, )+"
    End If
    
    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = Pattern
        If .test(sInp) Then
            Temp = .Replace(sInp, "$2/")
            ReplaceComma = Replace$(Temp, "/ ", "/")
        End If
    End With
End Function


Excel 2010
AB
1Quick Turn 15, 18, 20, 200 Dual Turn 20 8 Station, 8 Position QT Nexus 150/200/250/300/350 SQT 200, 250 12 Station, 12 Position QT Smart 150S / 300 / 350Quick Turn 15/18/20/200 Dual Turn 20 8 Station, 8 Position QT Nexus 150/200/250/300/350 SQT 200/250 12 Station, 12 Position QT Smart 150S /300 /350
Sheet1
Cell Formulas
RangeFormula
B1=ReplaceComma(A1)
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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