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/"
 

Sandeep Warrier

Board Regularmmmm Pizza
Joined
Oct 31, 2008
Messages
2,672
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
<b>Excel 2010</b><table cellpadding="2.5px" rules="all" style=";background-color: #FFFFFF;border: 1px solid;border-collapse: collapse; border-color: #BBB"><colgroup><col width="10px" style="background-color: #DAE7F5" /><col /><col /></colgroup><thead><tr style=" background-color: #DAE7F5;text-align: center;color: #161120"><th></th><th>A</th><th>B</th></tr></thead><tbody><tr ><td style="color: #161120;text-align: center;">1</td><td style=";">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</td><td style=";">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</td></tr></tbody></table><p style="width:3.6em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid #BBB;border-top:none;text-align: center;background-color: #DAE7F5;color: #161120">Sheet1</p><br /><br /><table width="85%" cellpadding="2.5px" rules="all" style=";border: 2px solid black;border-collapse:collapse;padding: 0.4em;background-color: #FFFFFF" ><tr><td style="padding:6px" ><b>Worksheet Formulas</b><table cellpadding="2.5px" width="100%" rules="all" style="border: 1px solid;text-align:center;background-color: #FFFFFF;border-collapse: collapse; border-color: #BBB"><thead><tr style=" background-color: #DAE7F5;color: #161120"><th width="10px">Cell</th><th style="text-align:left;padding-left:5px;">Formula</th></tr></thead><tbody><tr><th width="10px" style=" background-color: #DAE7F5;color: #161120">B1</th><td style="text-align:left">=ReplaceComma(<font color="Blue">A1</font>)</td></tr></tbody></table></td></tr></table><br />
 

Forum statistics

Threads
1,082,151
Messages
5,363,430
Members
400,736
Latest member
Aida

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top