separate alphanumeric value with a space

agid

New Member
Hi every one!

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

eg:

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

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

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use \$ signs: \$V\$2:\$Z\$99 will always point to V2:Z99, even after copying
Try:

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

Use it on a worksheet like:

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``````

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)

THANKS a lot!!!!

You people are great!!!!!

TONS of THANKS!!!!

Replies
1
Views
479
Replies
6
Views
315
Replies
7
Views
1K
Replies
6
Views
610
Replies
2
Views
323

1,221,424
Messages
6,159,824
Members
451,591
Latest member
j0eyjedi

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.

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

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