引用由表定义的范围中的一列

时间:2015-12-14 12:17:52

标签: excel excel-vba range vba

如何引用表定义的范围内的单个列?

Dim LandscapingDataRange As Range
Set LandscapingDataRange = ThisWorkbook.Worksheets(Sheet6.Name).ListObjects("TableReportLandscaping").DataBodyRange

LandscapingSiteMatch = Application.WorksheetFunction.Match(ContactListRange(MailCounter, 3), LandscapingDataRange(3), 0

我想仅在LandscapingDataRange中引用第三列,但上面只涉及一个单元格。

1 个答案:

答案 0 :(得分:0)

在我的大部分编程中,我都非常努力不要迷惑自己:)所以我通常会初始化一组"标准"对象提供我需要的访问权限。根据表中数据和列的更改频率,我有时会选择按名称而不是数字来访问列。

Option Explicit

Sub test()
    Dim ws As Worksheet
    Dim tblLandscaping As ListObject
    Dim tblLandscapingDBR As Range
    Dim tblLandscapingDataCol As Range
    Dim colName As String

    Set ws = ActiveSheet
    Set tblLandscaping = ws.ListObjects("TableReportLandscaping")
    Set tblLandscapingDBR = tblLandscaping.DataBodyRange
    Debug.Print "address range of DataBodyRange      = " & tblLandscapingDBR.Address & _
                " num rows = " & tblLandscapingDBR.Rows.Count & _
                " cols = " & tblLandscapingDBR.Columns.Count
    Set tblLandscapingDataCol = tblLandscaping.ListColumns(3).DataBodyRange
    Debug.Print "address range of column 3           = " & tblLandscapingDataCol.Address & _
                " num rows = " & tblLandscapingDataCol.Rows.Count

    colName = tblLandscaping.Name & "[Contracts]"
    Set tblLandscapingDataCol = ws.Range(colName)
    Debug.Print "address range of column 'Contracts' = " & tblLandscapingDataCol.Address & _
                " num rows = " & tblLandscapingDataCol.Rows.Count
End Sub