Jul 07 2021 06:37 PM
I am wanting to custom format a cell so when dta is entered, it converts to show a different format as such:
Data entered: A23456789
Desired display: A23 456 789
Essentially I would like a space every third character. All data will begin with a letter followed by 8 numbers.
Is what I want to do possible?
Jul 08 2021 12:10 AM
Custom number formats only apply to numbers and dates, not to text values. You'd have to use VBA code to do what you want. Let's say you want to insert spaces in column A. from cell A2 down.
Right-click the sheet tab.
Select 'View Code' from the context menu.
Copy the following code into the worksheet module:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Dim s As String
Dim t As String
Dim i As Long
If Not Intersect(Range("A2:A" & Rows.Count), Target) Is Nothing Then
Application.ScreenUpdating = False
Application.EnableEvents = False
For Each rng In Intersect(Range("A2:A" & Rows.Count), Target)
s = rng.Value
If s <> "" Then
If InStr(s, " ") = 0 Then
t = ""
For i = Len(s) - 2 To 1 Step -3
t = Mid(s, i, 3) & " " & t
Next i
If Len(s) Mod 3 Then
t = Left(s, Len(s) Mod 3) & " " & t
End If
rng.Value = t
End If
End If
Next rng
Application.EnableEvents = True
Application.ScreenUpdating = True
End If
End Sub
Switch back to Excel.
Save the workbook as a macro-enabled workbook (.xlsm)
Make sure that you allow macros when you open it.