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

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
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

doofusboy

Well-known Member
Joined
Oct 14, 2003
Messages
1,325
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

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
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,190,916
Messages
5,983,572
Members
439,850
Latest member
suhailrocks786

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
Top