Excel VBA - 在特定的非默认(A1)位置创建表

时间:2017-06-15 17:36:06

标签: excel vba excel-vba excel-2016 listobject

如果您运行以下代码:

WorkSheet.ListObjects.add(SourceType:=xlSrcRange, Destination:=Range("A10:C13"))

有人可能会想到,这会将表放在“A10:C13”的范围内。它没有,而是将表插入A1中,其中包含一列和一行(不包括标题):

enter image description here

在此功能的Official Documentation中明确指出:

  

如果SourceType设置为,则忽略Destination参数   xlSrcRange。

如何将表格插入任何其他细胞范围?

2 个答案:

答案 0 :(得分:2)

您正在将工作表范围定义添加到错误的参数中。

WorkSheet.ListObjects.add SourceType:=xlSrcRange, Source:=WorkSheet.Range("A10:C13")

有关该方法的完整说明,请参阅ListObjects.Add Method (Excel)

答案 1 :(得分:0)

使用this page提供的文档,我模拟了用户添加表格所能做的事情:

Range("A10:C13").Select
WorkSheet.ListObjects.add(SourceType:=xlSrcRange)

似乎添加select语句会将表放入正确的位置。