gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:09:14 25 4
gpt4 key购买 nike

我有一份报告,导出时会在单个单元格中将订单号(始终为 7 位长)显示为单个字符串。例如:订单 1234567 和 9876543 将在单个单元格中显示为 12345679876543。每个单元格的订单数量没有上限,每个单元格的订单数都不同。

有没有办法可以每 7 位数字添加一个字符,以便之后可以在列中添加文本?​​

最佳答案

为了避免使用又长又复杂的公式,我建议使用 VBA。

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

=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

关于vba - Excel/VBA - 如何在字符串中每 N 个字符插入一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43249900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com