VBA Code to Copy and past text from multiple cells in one table to single cell in a different table

My question will be very easy to understand, if you look at the diagram.
I have two tables in my word document. Table 1 has only two rows.
Table 2 has six rows.
I would like to have Macro that will copy text with its formating from from the first cell in Row 2, 3, 5 and 6 of Table 2 to the first cell in Row 2 of the First Table. The text from different cells in Table 2 will be pasted one below the other with manual line breaks. Image to show how the data will be before and after running the macro
Thank you for your help.
LVL 1
FaheemAhmadGulAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
See if this works in your situation:
Sub TabelToTable()
    Dim tbl1 As Table
    Dim tbl2 As Table
    
    Dim rng1 As Range
    Dim rng2 As Range
    
    Dim r1 As Integer
    Dim r2 As Integer
    
    Set tbl1 = ActiveDocument.Tables(1)
    Set tbl2 = ActiveDocument.Tables(2)
    
    r1 = 1
    r2 = 1
    
    Do Until r2 > tbl2.Rows.Count
        Set rng2 = tbl2.Cell(r2, 1).Range
        If Len(rng2) > 2 Then
            rng2.MoveEnd wdCharacter, -1 'drop trailing cell divider
            rng2.Copy
            
            Set rng1 = tbl1.Cell(r1, 1).Range
            If Len(rng1) > 2 Then 'add a new paragraph if cell not blank
                rng1.Collapse wdCollapseEnd
                rng1.Move wdCharacter, -1
                rng1.InsertAfter vbCr
            End If
            Set rng1 = tbl1.Cell(r1, 1).Range
            rng1.Collapse wdCollapseEnd
            Do While rng1.Cells(1).ColumnIndex > 1
                rng1.MoveEnd wdCharacter, -1 'come back to column 1
            Loop
            rng1.Paste

        End If
        r2 = r2 + 1
    Loop
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
FaheemAhmadGulAuthor Commented:
Many thanks. After a small modification it has worked perfectly and solved my problem. Thank you again. As ever I am very grateful.
0
GrahamSkanRetiredCommented:
In answering your next question(https://www.experts-exchange.com/questions/29070873/VBA-Code-to-copy-Text-from-different-rows-in-one-table-to-a-single-row-in-a-different-table.html), I noticed that there was a slight error in the code for this one.
I had started in row 1 for each table, instead of row 2. Here is a correction.
Option Explicit

Sub TabelToTable()
    Dim tbl1 As Table
    Dim tbl2 As Table
    
    Dim rng1 As Range
    Dim rng2 As Range
    
    Dim r1 As Integer
    Dim r2 As Integer
    
    Set tbl1 = ActiveDocument.Tables(1)
    Set tbl2 = ActiveDocument.Tables(2)
    
    r1 = 2
    r2 = 2
    
    Do Until r2 > tbl2.Rows.Count
        Set rng2 = tbl2.Cell(r2, 1).Range
        If Len(rng2) > 2 Then
            rng2.MoveEnd wdCharacter, -1 'drop trailing cell divider
            rng2.Copy
            
            Set rng1 = tbl1.Cell(r1, 1).Range
            If Len(rng1) > 2 Then 'add a new paragraph if cell not blank
                rng1.Collapse wdCollapseEnd
                rng1.Move wdCharacter, -1
                rng1.InsertAfter vbCr
            End If
            Set rng1 = tbl1.Cell(r1, 1).Range
            rng1.Collapse wdCollapseEnd
            Do While rng1.Cells(1).ColumnIndex > 1
                rng1.MoveEnd wdCharacter, -1 'come back to column 1
            Loop
            rng1.Paste

        End If
        r2 = r2 + 1
    Loop
End Sub

Open in new window

0
GrahamSkanRetiredCommented:
I guess that was the small modification that you mentioned in your last comment (that I have only just noticed).
0
FaheemAhmadGulAuthor Commented:
Thank you. Yes, that was the change I also made and after that it works fine.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.