Vba Excel将数据从listobject A(tableA)的几列中逐个复制到listobject B(tableB)的一列中
我有一个工作簿,包含多张工作表,每张工作表都包含一个listobject(一个表) 我的目的是从不同的列表对象中获取数据,并将其粘贴到另一个listobject中。或者更准确地说,在另一个表中逐个复制多个表的几列 到目前为止,我一直在尝试:Vba Excel将数据从listobject A(tableA)的几列中逐个复制到listobject B(tableB)的一列中,vba,excel,listobject,Vba,Excel,Listobject,我有一个工作簿,包含多张工作表,每张工作表都包含一个listobject(一个表) 我的目的是从不同的列表对象中获取数据,并将其粘贴到另一个listobject中。或者更准确地说,在另一个表中逐个复制多个表的几列 到目前为止,我一直在尝试: 'defining the names of the list objects Dim OriginTable As ListObject Set OriginTable = ThisWorkbook.Sheets("claims").ListObjects
'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
destinationTabl.ListRows.Add
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)
这是行不通的。
原因是destinationTable.listcolumns(1).databodyrange.item(lastitem)
不是列的最后一个单元格的范围,而是其他内容
我在玩range(),.address属性等,读了一些其他stackoverflow问题,但没有用
有人能帮忙吗?
简而言之,获取一个范围的日期并将其从另一个listobject的最后一列单元格粘贴
我正试图避免使用.select,因为选择总是有问题的。您能用With换行,然后使用
dataodyrange.Rows.Count
获取列中的最后一个单元格吗?此处说明的原则:
Sub test()
With ActiveSheet.ListObjects(1).ListColumns(1).DataBodyRange
Debug.Print .Cells(.Rows.Count, 1).Address
End With
End Sub
感谢@QHarr,我得到了答案:
With COMMtempTbl.ListColumns(1).DataBodyRange
originTable.ListColumns("comm").DataBodyRange.Copy Destination:=sheets("destination").range(.Cells(.Rows.Count + 1, 1).address)
End With
解释。
原点复制范围已清除
目标范围是通过获取目标表第1列的最后一个单元格的地址来完成的。但您必须将其引用到相应的工作表,因为地址的范围将引用到活动工作表,而不必引用到目标工作表
在(.Rows.Count+1,1)中,可以用相应的列号替换第二个1。我刚刚尝试了以下方法:origintable.ListColumns(“comm”).DataBodyRange.Copy Destination:=destinationTable.ListColumns(1).DataBodyRange.Cells(.Rows.Count,1).address,它给出了错误:-(您尝试根据您的情况调整它?发生了什么?我想原因是我上面写的。单元格(.rows.count,1)。地址是单元格的地址,但不是该单元格的范围。请在with语句中使用它,以确保使用正确的父级。)