我有一個報告說出口時,它在爲一個單一字符串的單個單元格顯示訂單號(誰是永遠的7位長)。例如:訂單1234567和9876543將在單個單元格中顯示爲12345679876543。每個單元不存在最大數量的訂單,它在每個單元上都不相同。的Excel/VBA - 如何在一個字符串插入字符每N個字符
有什麼辦法,我可以添加一個字符,每7位,這樣我可以做一個文本列之後?
我有一個報告說出口時,它在爲一個單一字符串的單個單元格顯示訂單號(誰是永遠的7位長)。例如:訂單1234567和9876543將在單個單元格中顯示爲12345679876543。每個單元不存在最大數量的訂單,它在每個單元上都不相同。的Excel/VBA - 如何在一個字符串插入字符每N個字符
有什麼辦法,我可以添加一個字符,每7位,這樣我可以做一個文本列之後?
要避免使用長且複雜的公式,我建議使用VBA。
下面的代碼粘貼到一個標準的模塊,然後就可以在工作表上使用的公式是這樣的:
=InsertPipe(A1)
Function InsertPipe(s As String)
Dim i As Long
Dim result As String
For i = 1 To Len(s) Step 7
On Error Resume Next
result = result & Left(s, 7) & "|"
s = Mid(s, 8, Len(s) - 7)
Next i
InsertPipe = Left(result, Len(result) - 1)
End Function
你可以使用CONCATENATE
。
防爆如果值是在兩個單元:=CONCATENATE(A1,",",B1)
防爆如果值是一個細胞=IF(LEN(A1)>7,CONCATENATE(LEFT(A1,7),",",MID(A1,8,100)))
編輯ADD
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
這真棒,非常感謝,它效果很好! – Jonorl