vba / udf to add space

Endeavouring

Board Regular
Joined
Jun 30, 2010
Messages
115
Hi

I need to add a space between text and numbers in any cell in a single column. Either the numeric or alpha could be first and each cell could contain any combination of alpha/numeric. Such as

A12
A12xyz
ABC123xyz
abc1234567890xyz9876543

Which would need changing to
A 12
A 12 xyz
ABC 123 xyz
abc 1234567890 xyz 9876543

If possible I am trying to make it either a VBA Sub or UDF

Any help would be appreciated as I've been batting this around with no success.
 
It was needed to trim result string:
Code:
Function AddSpace(Str As String) As String

    Application.Volatile
    
    Dim s As String
    Dim i As Integer
    Dim nextChar As String, currChar As String

    If Len(Str) < 2 Then
        AddSpace = Str
        Exit Function
    End If
    
    For i = 1 To Len(Str)
        
        nextChar = Mid(Str, i + 1, 1)
        currChar = Mid(Str, i, 1)
        
        [COLOR="Blue"]If (IsNumeric(currChar) And (Not IsNumeric(nextChar))) Or _
           (Not IsNumeric(currChar) And IsNumeric(nextChar)) Then[/COLOR]
            s = s & currChar & " "
        Else
            s = s & currChar
        End If
    Next
   
    AddSpace = [B]Trim[/B](s)

End Function
Just an observation on this code. I think the blue line could be condensed to
Rich (BB code):
If IsNumeric(currChar) + IsNumeric(nextChar) = -1 Then
 
Upvote 0

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Peter_SSs,
Yes, it's more compact. Just had no time to optimize it. ;)
 
Upvote 0

Forum statistics

Threads
1,225,157
Messages
6,183,247
Members
453,152
Latest member
ChrisMd

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