Home » excel » vba – Excel copy data from several columns of listobject A (tableA) into one column of listobject B (tableB) one after the other

vba – Excel copy data from several columns of listobject A (tableA) into one column of listobject B (tableB) one after the other

Posted by: admin May 14, 2020 Leave a comment


I have a workbook containing several sheets each one including a listobject (a table)

My intention is to get data from different list objects and paste it in another listobject. Or more precisely copying several columns of several tables one after the other in other table.

So far I was trying this:

'defining the names of the list objects
Dim OriginTable As ListObject
Set OriginTable = ThisWorkbook.Sheets("claims").ListObjects(1)
Dim destinationTable  As ListObject
Set destinationTable = ThisWorkbook.Sheets("COMM").ListObjects(1)

'inserting one row in case the table is empty.
If destinationTabl.ListColumns(1).DataBodyRange Is Nothing Then
End If

lastItem = destinationTable.ListColumns(1).DataBodyRange.Count+1
MsgBox ("I am going to insert in: ", lastItem)
originTable.ListColumns("comm").DataBodyRange.Copy Destination:=destinationTable.listcolumns(1).databodyrange.item(lastitem)

This does not work.
Reason being that destinationTable.listcolumns(1).databodyrange.item(lastitem) is not THE range of the last cell of the column, but something else.

I was playing around with range(), .address property etc, read some other stackoverflow questions at no avail.

Could someone help?
IN short again, getting the date of a range and paste it from the last column cell in another listobject

I am trying to avoid using .select since selecting is always problematic.

How to&Answers:

Can you wrap in a With and then use the DataBodyRange.Rows.Count to get the last cell in column? Principle illustrated here:

Sub test()
    With ActiveSheet.ListObjects(1).ListColumns(1).DataBodyRange
        Debug.Print .Cells(.Rows.Count, 1).Address
    End With
End Sub


Thanks to @QHarr I got the answer:

With COMMtempTbl.ListColumns(1).DataBodyRange
originTable.ListColumns("comm").DataBodyRange.Copy Destination:=sheets("destination").range(.Cells(.Rows.Count + 1, 1).address)
End With

the origin copy range is clear.

the destination range is done by getting the address of the last cell of the column 1 of the destination table. BUT you have to refer it to the corresponing sheet, since the range of an addres would be refered to the active sheet, and not necessary the destination sheet.

In (.Rows.Count + 1, 1) it would be possible to substitute the second 1 by the corresponding column number.