简体   繁体   中英

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'. The columns 'ClassProperty' & 'DataType' are adjacent.

How can I set the rng to the 'combined area/range' of the 2 columns mentioned using the column names?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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