跳到主要內容

如何在單元格中每x個字符添加/插入某些字符?

假設我有一個文本字符串列表,我想在每四個字符插入單元格後插入破折號或任何其他分隔符,以獲取以下屏幕截圖結果。 插入特定字符所需的單元格過多,是否有快速方法在Excel中每四個字符插入破折號?

使用VBA代碼將每個x字符中的某些字符插入到單元格中

使用Kutools for Excel將每個x字符中的某些字符插入單元格

doc插入每個x字符1


使用VBA代碼將每個x字符中的某些字符插入到單元格中

您可以應用以下VBA代碼在單元格中每四個字符後插入一個特定字符,請執行以下操作:

1。 按住 ALT + F11 鍵,然後打開 Microsoft Visual Basic for Applications 窗口。

2。 點擊 插入 > 模塊,然後將以下代碼粘貼到 模塊 窗口。

VBA代碼:每x個字符將某些字符插入到單元格中

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。 然後按 F5 鍵來運行此代碼,然後會彈出一個提示框,提醒您選擇要在文本字符串中插入某些字符的數據范圍,請參見屏幕截圖:

doc插入每個x字符2

4。 並點擊 OK,請在以下提示框中輸入您要在其後插入某些字符的數字,請參見屏幕截圖:

doc插入每個x字符3

5。 繼續點擊 OK,然後在隨後的提示框中,輸入要添加到文本中的特定字符,請參見屏幕截圖:

doc插入每個x字符4

6。 點擊 OK 按鈕,然後在提示框中選擇要在其中輸出結果的一個單元格,請參見屏幕截圖:

doc插入每個x字符5

7。 最後點擊 OK,該字符已每四個字符插入到每個文本字符串中,請參見屏幕截圖:

doc插入每個x字符6


使用Kutools for Excel將每個x字符中的某些字符插入單元格

對於大多數Excel初學者來說,上面的代碼可能有些困難,在這裡,我可以介紹一個方便的工具- Excel的Kutools,其 添加文本 功能,您可以在單元格值的之前,之後或某些位置快速添加特定的文本或字符。

Excel的Kutools : 帶有300多個便捷的Excel加載項,可以在30天內免費試用. 

如果你已經安裝 Excel的Kutools,請執行以下操作:

1。 選擇要插入特定字符的數據范圍。

2。 然後點擊 庫工具 > 文本 > 添加文本,請參見屏幕截圖:

3。 在 添加文本 對話框中,指定要插入的字符 文本 框,然後選擇 從位置指定 部分,在文本框中輸入要在其後插入字符的位置,同時,您可以在右側窗格中預覽結果,請參見屏幕截圖:

doc插入每個x字符8

保養竅門:在這裡,我在的文本框中輸入4,8,12 指定 表示在文本字符串的第4、8和12個字符之後插入特定文本。

4. 然後點擊 Ok or 登記 按鈕,指定的字符已插入到單元格值中的特定位置。

doc插入每個x字符9 2 doc插入每個x字符10

單擊以了解有關此“添加文本”實用程序的更多詳細信息。

立即下載和免費試用Excel的Kutools!


演示:使用Kutools for Excel將每x個字符中的某些字符插入單元格

Excel的Kutools:具有300多個方便的Excel加載項,可以在30天內免費試用,沒有任何限制。 立即下載並免費試用!

最佳辦公生產力工具

🤖 Kutools 人工智慧助手:基於以下內容徹底改變數據分析: 智慧執行   |  生成代碼  |  建立自訂公式  |  分析數據並產生圖表  |  呼叫 Kutools 函數...
熱門特色: 尋找、突出顯示或識別重複項   |  刪除空白行   |  合併列或儲存格而不遺失數據   |   沒有公式的回合 ...
超級查詢: 多條件VLookup    多值VLookup  |   跨多個工作表的 VLookup   |   模糊查詢 ....
高級下拉列表: 快速建立下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
欄目經理: 新增特定數量的列  |  移動列  |  切換隱藏列的可見性狀態  |  比較範圍和列 ...
特色功能: 網格焦點   |  設計圖   |   大方程式酒吧    工作簿和工作表管理器   |  資源庫 (自動文字)   |  日期選擇器   |  合併工作表   |  加密/解密單元格    按清單發送電子郵件   |  超級濾鏡   |   特殊過濾器 (過濾粗體/斜體/刪除線...)...
前 15 個工具集12 文本 工具 (添加文本, 刪除字符,...)   |   50+ 圖表 類型 (甘特圖,...)   |   40+ 實用 公式 (根據生日計算年齡,...)   |   19 插入 工具 (插入二維碼, 從路徑插入圖片,...)   |   12 轉化 工具 (數字到單詞, 貨幣兌換,...)   |   7 合併與拆分 工具 (高級合併行, 分裂細胞,...)   |   ... 和更多

使用 Kutools for Excel 增強您的 Excel 技能,體驗前所未有的效率。 Kutools for Excel 提供了 300 多種進階功能來提高生產力並節省時間。  點擊此處獲取您最需要的功能...

產品描述


Office選項卡為Office帶來了選項卡式界面,使您的工作更加輕鬆

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
  • 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
  • 將您的工作效率提高 50%,每天為您減少數百次鼠標點擊!
Comments (3)
No ratings yet. Be the first to rate!
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.
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
buenas noches
necesito que me coloque el caracter cada 6, 7, 8 de un solo clic como le modifico para poder colocar mas criterios
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations