简体   繁体   中英

Excel concatenate multi-line text with next column multi-line

I have a long 2 columns running into thousands in each column varied length of multi-line text is present i can concatenate if only one line is present but how to concatenate via VBA when one cell have multi-line text to repective multi-line text. it can be seen like below

sample output with raw data

在此处输入图片说明

sample file is 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

You can use Split() to divide each cell content into multiple strings, and then go from there as you can see in Concatenate_Multiline() .

I included a Test() method, it will take multiline text from A1 (first parameter) and multiline text from B1 (2nd parameter) and will put the concatenated result in C1 (third parameter).

Sub Test()
    For i = 1 To Rows.Count
        Concatenate_Multiline Range("A" & i), Range("B" & i), Range("C" & i)
    Next i
End Sub

Sub Concatenate_Multiline(cell1 As Range, cell2 As Range, destination As Range)
    Dim lineCell1() As String
    Dim lineCell2() As String
    Dim sResult As String

    lineCell1() = Split(cell1.Formula, vbLf, , vbTextCompare)
    lineCell2() = Split(cell2.Formula, vbLf, , vbTextCompare)

    For i = LBound(lineCell1) To UBound(lineCell1)
        sResult = sResult & lineCell1(i)

        If (i >= LBound(lineCell2)) Then
            If (i <= UBound(lineCell2)) Then
                sResult = sResult & lineCell2(i)

                If (i < UBound(lineCell1)) Then
                    sResult = sResult & vbLf
                End If
            End If
        End If
    Next i

    destination.Formula = sResult
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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