简体   繁体   English

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

[英]Excel/VBA - How to insert a character in a string every N characters

I have a report that when exported it displays order numbers (who are always 7 digit long) in a single cell as one single string. 我有一个报告,它在导出时会在一个单元格中显示一个单个字符串的订单号(总是7位长)。 Eg: orders 1234567 and 9876543 will appear as 12345679876543 in a single cell. 例如:订单1234567和9876543将在单个单元格中显示为12345679876543。 There isn't a maximum number of orders per cell, it varies on every cell. 每个单元格没有最大订单数,每个单元格都有所不同。

Is there any way that I can add a character every 7 digits so that I can do a text to columns afterwards? 有没有什么方法可以每7位数添加一个字符,以便我可以在之后为列做文本?

To avoid using a long and complicated formula, I'd suggest using VBA. 为了避免使用冗长而复杂的公式,我建议使用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

You could use CONCATENATE . 你可以使用CONCATENATE

Ex if values are in two cells: =CONCATENATE(A1,",",B1) 如果值在两个单元格中,则为Ex: =CONCATENATE(A1,",",B1)

Ex if values are in one cell =IF(LEN(A1)>7,CONCATENATE(LEFT(A1,7),",",MID(A1,8,100))) Ex如果值在一个单元格中=IF(LEN(A1)>7,CONCATENATE(LEFT(A1,7),",",MID(A1,8,100)))

EDIT ADD 编辑添加

VBA-Code you could use (found a while ago) 您可以使用的VBA代码(刚才发现)

 Sub AddACharacter()

Dim Rng As Range
Dim InputRng As Range, OutRng As Range
Dim Row As Integer
Dim Char As String
Dim Index As Integer
Dim arr As Variant
Dim Val As String
Dim OutVal As String
Dim Num As Integer
xTitleId = "Add a character"
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Range :", xTitleId, InputRng.Address,Type:=8)
Row = Application.InputBox("Number of characters :", xTitleId, Type:=1)
Char = 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")
Num = 1
For Each Rng In InputRng
Val = Rng.Value
OutVal = ""
For Index = 1 To VBA.Len(Val)
    If Index Mod Row = 0 And Index <> VBA.Len(Val) Then
        OutVal = OutVal + VBA.Mid(Val, Index, 1) + Char
    Else
        OutVal = OutVal + VBA.Mid(Val, Index, 1)
    End If
Next
OutRng.Cells(Num, 1).Value = OutVal
Num = Num + 1
Next
End Sub

You can use a formula copied accross and down, but would be finite in relation to splitting expected, you could use something along these lines, in b1 to z1 in my example. 你可以使用一个被复制的公式,但是在预期的分割方面是有限的,你可以在这些行中使用某些东西,在我的例子中是b1到z1。

=MID($A1,IF(COLUMN()-2=0,1,((COLUMN()-2)*7)+1),7)

在此输入图像描述

Assuming the string is in A2, then try this... In B2 假设字符串在A2中,那么尝试这个......在B2中

=SUBSTITUTE(A2,LEFT(A2,7),LEFT(A2,7)&"|")

The above formula will insert a "|" 上面的公式将插入一个“|” after seven characters. 七个字后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM