separate alphanumeric value with a space

agid

New Member
Joined
Mar 13, 2009
Messages
2
Hi every one!

I am looking for a formula or VBA code in Excel to separate alphanumeric value with a space

eg: :confused:

asd123df34a ----to----> asd 123 df 34 a

123abc5 ----to----> 123 abc 5

Thanks in advance!

Awaiting a reply :)
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Try:

Code:
Function AddSpaces(Txt As String)
    Dim i As Integer
    AddSpaces = Left(Txt, 1)
    For i = 2 To Len(Txt)
        If IsNumeric(Mid(Txt, i, 1)) Then
            If Not IsNumeric(Mid(Txt, i - 1, 1)) Then
                AddSpaces = AddSpaces & " " & Mid(Txt, i, 1)
            Else
                AddSpaces = AddSpaces & Mid(Txt, i, 1)
            End If
            If Not IsNumeric(Mid(Txt, i + 1, 1)) Then
                AddSpaces = AddSpaces & " "
            End If
        Else
            AddSpaces = AddSpaces & Mid(Txt, i, 1)
        End If
    Next i
End Function

Use it on a worksheet like:

=AddSpaces(A1)
 
Upvote 0
Assuming the target cell is "A1", this should work:

Code:
Dim iCTR As Integer
Dim previousCharacter As Boolean
Dim newValue As String
    For iCTR = 1 To Len(Range("A1").Value)
        If iCTR = 1 Then
            previousCharacter = IsNumeric(Mid(Range("A1").Value, iCTR, 1))
            newValue = Mid(Range("A1").Value, iCTR, 1)
        End If
        If iCTR > 1 Then
            If IsNumeric(Mid(Range("A1").Value, iCTR, 1)) = previousCharacter Then
                newValue = newValue & Mid(Range("A1").Value, iCTR, 1)
                previousCharacter = IsNumeric(Mid(Range("A1").Value, iCTR, 1))
            Else
                newValue = newValue & " " & Mid(Range("A1").Value, iCTR, 1)
                previousCharacter = IsNumeric(Mid(Range("A1").Value, iCTR, 1))
            End If
        End If
    Next iCTR
    Range("A1").Value = newValue
 
Upvote 0
Here's one using Regexp:

Code:
Function Ins_Space(s As String)
With CreateObject("vbscript.regexp")
    .Global = True
    .Pattern = "(\d+)"
    Ins_Space = Trim(.Replace(s, " $1 "))
End With
End Function

Use like:

=Ins_Space(A1)
 
Upvote 0

Forum statistics

Threads
1,214,945
Messages
6,122,395
Members
449,081
Latest member
JAMES KECULAH

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