How to insert / add space after every x characters in cells?

If you have multiple text strings in a list of worksheet, now, you want to insert space after every 3 or x characters in cells as following screenshot shown. How could you solve this problem in Excel quickly and easily?

doc add space after x characters 1

Insert / Add space after every x characters in cells with VBA code

Insert / Add space after every x characters in cells with Kutools for Excel


arrow blue right bubble Insert / Add space after every x characters in cells with VBA code

Applying the following VBA code, you can quickly insert the space after every n characters for multiple cells at once, please do as follows:

1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.

2. Then click Insert > Module, and paste the following code in the Module window.

VBA code: Insert space after every x characters:

Sub addspace()
'updateby Extendoffice 20151207
    Dim i As Long
    Dim xCell As Range
    Dim xRg As Range
    Dim xTxt As String
    Dim xStr As String
    Dim xUpdate As Boolean
    On Error Resume Next
    xTxt = ActiveWindow.RangeSelection.Address
    Set xRg = Application.InputBox("Please select the data range:", "Kutools for Excel", xTxt, , , , , 8)
    Set xRg = Application.Intersect(xRg, xRg.Worksheet.UsedRange)
    If xRg Is Nothing Then Exit Sub
    xUpdate = Application.ScreenUpdating
    Application.ScreenUpdating = False
    For Each xCell In xRg
        xStr = xCell.Text
        xTxt = ""
        For i = 1 To Len(xStr) Step 4
            If xTxt = "" Then
                xTxt = Mid(xStr, i, 4)
            Else
                xTxt = Trim(xTxt) & " " & Mid(xStr, i, 4)
            End If
        Next
        xCell = xTxt
    Next
    Application.ScreenUpdating = xUpdate
End Sub

3. Then press F5 key to run this code, and in the prompt box, please select the cells that you want to insert space to, see screenshot:

doc add space after x characters 2

4. And then click OK button, and the spaces have been inserted into the cell contents after every four characters, see screenshots:

doc add space after x characters 3  2 doc add space after x characters 4

Note: In the above code, you can change the number 4 to other number to insert space after. And also you can change the delimiter space to another delimiters in this rule xTxt = Trim(xTxt) & " " & Mid(xStr, i, 4) of the above code.


arrow blue right bubble Insert / Add space after every x characters in cells with Kutools for Excel

Kutools for Excel’s Add Text feature also can help you to solve this problem quickly and easily.

Kutools for Excel : with more than 300 handy Excel add-ins, free to try with no limitation in 60 days

After installing Kutools for Excel, please do as follows:( Free Download Kutools for Excel Now! )

1. Select the text strings that you want to insert space.

2. Then click Kutools > Text > Add Text, see screenshot:

doc add space after x characters 5

3. In the Add Text dialog box, press the space bar once into the Text box, and select the Specify option under the Position, then type the positions that you want to insert the space, see screenshot:

doc add space after x characters 6

4. And then click Ok or Apply button, you will get the result that the spaces have been inserted after every four characters as you need.

Tips: The number of positions in Specify textbox should be separated by commas.

Free Download Kutools for Excel Now!


Related article:

How to add / insert certain character every x characters into cells?


The Best Office Productivity Tools

Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%

  • Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...
  • Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
  • Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns... Prevent Duplicate Cells; Compare Ranges...
  • Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
  • Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
  • Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
  • Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
  • Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
  • More than 300 powerful features. Supports Office/Excel 2007-2019 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial. 60-day money back guarantee.
kte tab 201905

Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
officetab bottom
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.

Be the first to comment.