简体   繁体   English

如何使用 excel 中结构化引用表的特定连续列在 vba 中设置范围?

[英]How to set a range in vba using specific contiguous columns of structured reference table in excel?

I am getting 'Invalid procedure call or argument error..' when using the following code:使用以下代码时,我收到“无效的过程调用或参数错误..”:

Dim DTTbl As ListObject
Dim rng As Range

Set DTTbl = ThisWorkbook.Worksheets("DataTypes").ListObjects("DataTypes")
Set rng = DTTbl.Range("DataTypes[[ClassProperty]:[DataType]]")

Note that both the worksheet and the table are named 'DataTypes'.请注意,工作表和表格都命名为“DataTypes”。 The columns 'ClassProperty' & 'DataType' are adjacent. “ClassProperty”和“DataType”列是相邻的。

How can I set the rng to the 'combined area/range' of the 2 columns mentioned using the column names?如何将 rng 设置为使用列名提到的 2 列的“组合区域/范围”?

You can do it like this:你可以这样做:

Dim col1 As Range, col2 As Range
Set col1 = DTTbl.ListColumns("ClassProperty").DataBodyRange
Set col2 = DTTbl.ListColumns("DataType").DataBodyRange

Set rng = ThisWorkbook.Worksheets("DataTypes").Range(col1.Address, col2.Address)
Debug.Print rng.Address

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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