Note: The other languages of the website are Google-translated. Back to English
English English

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

Supposing, I have a list of text strings that I want to insert dashes or any other separators after every four characters into cells to get the following screenshot result. There are too many cells needed to insert the certain character, are there any quick ways to insert dashes every four character in Excel?

Insert certain character every x characters into cell with VBA code

Insert certain character every x characters into cell with Kutools for Excel

doc insert every x character 1


Insert certain character every x characters into cell with VBA code

You can apply the following VBA code to insert a specific character after every four characters into cells, please do as follows:

1. Hold down the ALT + F11 keys in Excel, and it opens the Microsoft Visual Basic for Applications window.

2. Click Insert > Module, and paste the following code in the Module Window.

VBA code: Insert certain character every x characters into cell

Sub InsertCharacter()
'Updateby Extendoffice
Dim Rng As Range
Dim InputRng As Range, OutRng As Range
Dim xRow As Integer
Dim xChar As String
Dim index As Integer
Dim arr As Variant
Dim xValue As String
Dim outValue As String
Dim xNum As Integer
xTitleId = "KutoolsforExcel"
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Range :", xTitleId, InputRng.Address, Type:=8)
xRow = Application.InputBox("Number of characters :", xTitleId, Type:=1)
xChar = Application.InputBox("Specify a character :", xTitleId, Type:=2)
Set OutRng = Application.InputBox("Out put to (single cell):", xTitleId, Type:=8)
Set OutRng = OutRng.Range("A1")
xNum = 1
For Each Rng In InputRng
    xValue = Rng.Value
    outValue = ""
    For index = 1 To VBA.Len(xValue)
        If index Mod xRow = 0 And index <> VBA.Len(xValue) Then
            outValue = outValue + VBA.Mid(xValue, index, 1) + xChar
        Else
            outValue = outValue + VBA.Mid(xValue, index, 1)
        End If
    Next
    OutRng.Cells(xNum, 1).Value = outValue
    xNum = xNum + 1
Next
End Sub

3. Then press F5 key to run this code, and a prompt box will pop out to remind you to select the data range that you want to insert certain character within the text strings, see screenshot:

doc insert every x character 2

4. And click OK, in the following prompt box, please enter the number that you want to insert certain character every after, see screenshot:

doc insert every x character 3

5. Go on clicking OK, and in the followed prompt box, please enter the certain character that you want to add to the text, see screenshot:

doc insert every x character 4

6. Click OK button, and choose one cell where you want to output the result in the prompt box, see screenshot:

doc insert every x character 5

7. At last, click OK, the certain character has been inserted into each text string every four characters, see screenshot:

doc insert every x character 6


Insert certain character every x characters into cell with Kutools for Excel

The above code maybe somewhat difficult for most Excel beginner, here, I can introduce a handy tool -- Kutools for Excel, with its Add Text feature, you can quickly add a specific text or character before, after or the certain positions of the cell value.

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

If you have installed Kutools for Excel, please do as follows:

1. Select the data range that you want to insert a specific character.

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

3. In the Add Text dialog box, specify a character that you want to insert in the Text box, and then choose Specify from the Position section, in the text box, enter the positions that you want to insert the character after, at the same time, you can preview the result at the right pane, see screenshot:

doc insert every x character 8

Tips: Here I type 4,8,12 in the textbox of Specify which indicate to insert the specific text after the 4th, 8th and 12th characters of the text string.

4. Then click Ok or Apply button, the specified character has been inserted into the cell values at specific positions.

doc insert every x character 9  2 doc insert every x character 10

Click to know more details about this Add Text utility.

Download and free trial Kutools for Excel Now !


Demo: Insert certain character every x characters into cell with Kutools for Excel

Kutools for Excel: with more than 300 handy Excel add-ins, free to try with no limitation in 30 days. Download and free trial Now!

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
Comments (3)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
buenas noches
necesito que me coloque el caracter cada 6, 7, 8 de un solo clic como le modifico para poder colocar mas criterios
This comment was minimized by the moderator on the site
Muy buena esta macro, una pregunta curiosa al momento de ejecutarla los caracteres los aplica de izquierda a derecha, puede cambiarse ese orden que de derecha a izquierda? En caso de ser posible como es el proceso? Gracias.
This comment was minimized by the moderator on the site
Creo que en vez de macros, es más fácil (aunque más largo):
1. Separar en columnas la columna A (Pestaña Datos - botón texto en columnas) y separar por Ancho fijo. Así podrás poner un separador cada 4 caracteres.
2. Elige destino desde B2 (si lo dejas predeterminado, pisará la columna A y no podrás ver el texto original).
3. Ahora viene lo bueno, ya que son 4 series de 4caracteres cada uno, en la columna G o H (considerando que los valores de 4 caracteres estén desde las columnas B a la F), colocarás la siguiente fórmula: =CONCATENAR(B2;"-";C2;"-";D2;"-";E2;"-";F2). Ahora copia esa celda al resto de valores por insertar el símbolo -.
4. Finalmente, copia toda la columna de resultados y pégala en una columna vacía pero como valor de texto (Inicio-Pegar-Valores... o con las teclas ALT+O+V+V) y listo, ya tienes tus seriales listos para pegar donde quieras sin necesidad de macros.
There are no comments posted here yet
Leave your comments
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations