且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Excel/VBA-如何在每N个字符的字符串中插入一个字符

更新时间:2022-12-28 16:10:00

为避免使用冗长而复杂的公式,建议使用VBA.

To avoid using a long and complicated formula, I'd suggest using VBA.

将下面的代码粘贴到标准模块中,然后可以在工作表上使用如下公式:

Paste the code below into a standard module and then you can use the formula like this on the worksheet:

=InsertPipe(A1,7)


Function InsertPipe(s As String, interval As Long)
    If interval < 1 Then Exit Function        

    Dim i As Long, result As String

    For i = 1 To Len(s) Step interval
        On Error Resume Next
        result = result & Left(s, interval) & "|"
        s = Mid(s, interval + 1, Len(s) - interval)
    Next i

    InsertPipe = Left(result, Len(result) - 1)
End Function