我有一长串2列在每列中运行数千个不同长度的多行文本存在我可以连接如果只有一行但是当一个单元格有多行文本时,如何通过VBA连接线文字。它可以在下面看到
带有原始数据的示例输出
示例文件为Raw excel File
Option Explicit
Sub Ampersander()
Call Concatenate_Formula(False, False)
End Sub
Sub Ampersander_Options()
Call Concatenate_Formula(False, True)
End Sub
Sub Concatenate()
Call Concatenate_Formula(True, False)
End Sub
Sub Concatenate_Options()
Call Concatenate_Formula(True, True)
End Sub
Sub Concatenate_Formula(bConcat As Boolean, bOptions As Boolean)
Dim rSelected As Range
Dim c As Range
Dim sArgs As String
Dim bCol As Boolean
Dim bRow As Boolean
Dim sArgSep As String
Dim sSeparator As String
Dim rOutput As Range
Dim vbAnswer As VbMsgBoxResult
Dim lTrim As Long
Dim sTitle As String
Set rOutput = ActiveCell
bCol = False
bRow = False
sSeparator = ""
sTitle = IIf(bConcat, "CONCATENATE", "Ampersand")
On Error Resume Next
Set rSelected = Application.InputBox(Prompt:= _
"Select cells to create formula", _
Title:=sTitle & " Creator", Type:=8)
On Error GoTo 0
If Not rSelected Is Nothing Then
sArgSep = IIf(bConcat, ",", "&")
If bOptions Then
vbAnswer = MsgBox("Columns Absolute? $A1", vbYesNo)
bCol = IIf(vbAnswer = vbYes, True, False)
vbAnswer = MsgBox("Rows Absolute? A$1", vbYesNo)
bRow = IIf(vbAnswer = vbYes, True, False)
sSeparator = Application.InputBox(Prompt:= _
"Type separator, leave blank if none.", _
Title:=sTitle & " separator", Type:=2)
End If
For Each c In rSelected.Cells
sArgs = sArgs & c.Address(bRow, bCol) & sArgSep
If sSeparator <> "" Then
sArgs = sArgs & Chr(34) & sSeparator & Chr(34) & sArgSep
End If
Next
lTrim = IIf(sSeparator <> "", 4 + Len(sSeparator), 1)
sArgs = Left(sArgs, Len(sArgs) - lTrim)
If bConcat Then
rOutput.Formula = "=CONCATENATE(" & sArgs & ")"
Else
rOutput.Formula = "=" & sArgs
End If
End If
End Sub
答案 0 :(得分:1)
您可以使用Split()
将每个单元格内容划分为多个字符串,然后从Concatenate_Multiline()
中看到。
我添加了Test()
方法,它将从A1
(第一个参数)和来自B1
(第二个参数)的多行文本中获取多行文本,并将连接结果放在{{ 1}}(第三个参数)。
C1