Add space between alphabets and numbers

Ks1102

Well-known Member
Joined
Jan 8, 2008
Messages
689
Dear Expert
can u help how to adding space if letter and number ? find the code below it just can only be added in last character's.

Example : the doe just be done "
postal620332 ste"
How can it done become "postal 620332 ste"
postal620332 ste

VBA Code:
Sub InsertSpaceBetweenDigitAndLetter()
  Dim X As Long, Z As Long, LastRow As Long, vArr As Variant
  Const DataColumn As String = "A"
  Const StartRow As Long = 1
  LastRow = Cells(Rows.Count, DataColumn).End(xlUp).Row
  vArr = Cells(StartRow, DataColumn).Resize(LastRow - StartRow + 1).Value
  For X = 1 To UBound(vArr)
    For Z = Len(vArr(X, 1)) - 1 To 1 Step -1
      If Mid(vArr(X, 1), Z, 1) Like "#" And Mid(vArr(X, 1), Z + 1, 1) Like "[A-Za-z]" Then
        vArr(X, 1) = Left(vArr(X, 1), Z) & " " & Mid(vArr(X, 1), Z + 1)
      End If
    Next
  Next
  Cells(StartRow, DataColumn).Resize(LastRow - StartRow + 1).NumberFormat = "@"
  Cells(StartRow, DataColumn).Resize(LastRow - StartRow + 1) = vArr
End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Try your code this way...
VBA Code:
Sub InsertSpaceBetweenDigitAndLetter()
  Dim X As Long, Z As Long, LastRow As Long, vArr As Variant
  Const DataColumn As String = "A"
  Const StartRow As Long = 1
  LastRow = Cells(Rows.Count, DataColumn).End(xlUp).Row
  With Cells(StartRow, DataColumn).Resize(LastRow - StartRow + 1)
    vArr = .Value
    For X = 1 To UBound(vArr)
      For Z = Len(vArr(X, 1)) - 1 To 1 Step -1
        If Mid(vArr(X, 1), Z, 2) Like "#[A-Za-z]" Or Mid(vArr(X, 1), Z, 2) Like "[A-Za-z]#" Then
          vArr(X, 1) = Left(vArr(X, 1), Z) & " " & Mid(vArr(X, 1), Z + 1)
        End If
      Next
    Next
    .NumberFormat = "@"
    .Cells = vArr
  End With
End Sub
 
Upvote 0
Try your code this way...
VBA Code:
Sub InsertSpaceBetweenDigitAndLetter()
  Dim X As Long, Z As Long, LastRow As Long, vArr As Variant
  Const DataColumn As String = "A"
  Const StartRow As Long = 1
  LastRow = Cells(Rows.Count, DataColumn).End(xlUp).Row
  With Cells(StartRow, DataColumn).Resize(LastRow - StartRow + 1)
    vArr = .Value
    For X = 1 To UBound(vArr)
      For Z = Len(vArr(X, 1)) - 1 To 1 Step -1
        If Mid(vArr(X, 1), Z, 2) Like "#[A-Za-z]" Or Mid(vArr(X, 1), Z, 2) Like "[A-Za-z]#" Then
          vArr(X, 1) = Left(vArr(X, 1), Z) & " " & Mid(vArr(X, 1), Z + 1)
        End If
      Next
    Next
    .NumberFormat = "@"
    .Cells = vArr
  End With
End Sub
Wow ... it's awesome .. thanks so much
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,547
Members
449,089
Latest member
davidcom

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