Adding a comma within a text string based on a condtion

I have a column of text.  Within the column for some cells I have embedded numbers.  My goal is if I find a cell where a space is found between two numbers I want to add a comma.  An example is below.  My thought was to use the InStr function within VBA (I already have an existing loop that goes thru the column), but this function only seems like a solution if I wanted to add a comma once I found a space.  However, I ONLY want to add a comma if a space is found within the string AND if the space is found between 2 numbers.

Favor18 500Bye
Chuckwagon100 000sick

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
Try this macro. Change the Const's as needed.

Sub AddComma()
Dim strParts() As String
Dim lngLastRow As Long
Dim lngRow As Long
Dim intPart As Integer
Dim strNew As String
Const DATA_COL = "A"
Const DEST_COL = "B"
Const FIRST_ROW = 1

With ActiveSheet
    lngLastRow = .Range(DATA_COL & "1048576").End(xlUp).Row
    For lngRow = FIRST_ROW To lngLastRow
        strNew = ""
        strParts = Split(.Cells(lngRow, DATA_COL), " ")
        If UBound(strParts) > 0 Then
            For intPart = 0 To UBound(strParts) - 1
                If IsNumeric(Right$(strParts(intPart), 1)) And IsNumeric(Left$(strParts(intPart + 1), 1)) Then
                    strNew = strNew & strParts(intPart) & "," & strParts(intPart + 1)
                    intPart = intPart + 1
                    strNew = strNew & strParts(intPart) & " " & strParts(intPart + 1)
                End If
            strNew = .Cells(lngRow, DATA_COL)
            .Cells(lngRow, DEST_COL) = strNew
        End If
End With
End Sub

Open in new window

Wayne Taylor (webtubbs)Commented:
Regular Expressions are handy in situations like this. In the below "\d" signifies a single digit and "\s" signifies a space.

Sub ReplaceSpace()

    Dim cell As Range
    Dim rx As Object
    Set rx = CreateObject("VBScript.RegExp")
    rx.Pattern = "\d\s\d"
    For Each cell In Selection
        If rx.Test(cell.Value) Then
            cell.Value = rx.Replace(cell.Value, Replace(rx.Execute(cell.Value).Item(0).Value, " ", ","))
        End If
End Sub

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
upobDaPlayaAuthor Commented:
Both approaches make sense - I used the reg solution
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.