39

Excel VBA 中是否可以引用命名表?

假设这可能是...

Sheets("Sheet1").Table("A_Table").Select

我看到有人提到表是一个列表对象,但我不确定这是否是同一回事。

4

6 回答 6

104

OP问,是否可以引用表格,而不是如何添加表格。所以工作相当于

Sheets("Sheet1").Table("A_Table").Select

将是这样的声明:

Sheets("Sheet1").ListObjects("A_Table").Range.Select

或选择零件(仅像表中的数据):

Dim LO As ListObject
Set LO = Sheets("Sheet1").ListObjects("A_Table")
LO.HeaderRowRange.Select        ' Select just header row
LO.DataBodyRange.Select         ' Select just data cells
LO.TotalsRowRange.Select        ' Select just totals row

对于这些部分,您可能希望在选择它们之前测试是否存在标题行和总计行。

说真的,这是在 SO 中在 VBA 中引用表的唯一问题吗?Excel 中的表格非常有意义,但在 VBA 中很难使用它们!

于 2014-06-20T22:38:57.683 回答
30

Excel 中的“表”确实称为 ListObject。

引用表的“正确”方法是从其工作表中获取其 ListObject ,即SheetObject.ListObjects(ListObjectName).

如果您想在不使用工作表的情况下引用表格,您可以使用 hack Application.Range(ListObjectName).ListObject

注意:此 hack 依赖于 Excel 始终为表的DataBodyRange创建与表同名的命名范围这一事实。但是,可以更改此范围名称......虽然这不是您想要做的事情,因为如果您编辑表名,名称将重置!您也可以获得一个没有关联ListObject的命名范围。

当您输入错误名称时,鉴于 Excel 的 1004 错误消息不是很有帮助,您可能需要创建一个包装器...

Public Function GetListObject(ByVal ListObjectName As String, Optional ParentWorksheet As Worksheet = Nothing) As Excel.ListObject
On Error Resume Next

    If (Not ParentWorksheet Is Nothing) Then
        Set GetListObject = ParentWorksheet.ListObjects(ListObjectName)
    Else
        Set GetListObject = Application.Range(ListObjectName).ListObject
    End If

On Error GoTo 0 'Or your error handler

    If (Not GetListObject Is Nothing) Then
        'Success
    ElseIf (Not ParentWorksheet Is Nothing) Then
        Call Err.Raise(1004, ThisWorkBook.Name, "ListObject '" & ListObjectName & "' not found on sheet '" & ParentWorksheet.Name & "'!")
    Else
        Call Err.Raise(1004, ThisWorkBook.Name, "ListObject '" & ListObjectName & "' not found!")
    End If

End Function

这里还有一些好的 ListObject 信息。

于 2015-02-03T03:53:03.220 回答
7

此外,定义引用对象的变量也很方便。例如,

Sub CreateTable()
    Dim lo as ListObject
    Set lo = ActiveSheet.ListObjects.Add(xlSrcRange, Range("$B$1:$D$16"), , xlYes)
    lo.Name = "Table1"
    lo.TableStyle = "TableStyleLight2"
    ...
End Sub

您可能会立即发现它很有利。

于 2013-11-20T20:20:57.650 回答
6

In addition to the above, you can do this (where "YourListObjectName" is the name of your table):

Dim LO As ListObject
Set LO = ActiveSheet.ListObjects("YourListObjectName")

But I think that only works if you want to reference a list object that's on the active sheet.

I found your question because I wanted to refer to a list object (a table) on one worksheet that a pivot table on a different worksheet refers to. Since list objects are part of the Worksheets collection, you have to know the name of the worksheet that list object is on in order to refer to it. So to get the name of the worksheet that the list object is on, I got the name of the pivot table's source list object (again, a table) and looped through the worksheets and their list objects until I found the worksheet that contained the list object I was looking for.

Public Sub GetListObjectWorksheet()
' Get the name of the worksheet that contains the data
' that is the pivot table's source data.

    Dim WB As Workbook
    Set WB = ActiveWorkbook

    ' Create a PivotTable object and set it to be
    ' the pivot table in the active cell:
    Dim PT As PivotTable
    Set PT = ActiveCell.PivotTable

    Dim LO As ListObject
    Dim LOWS As Worksheet

    ' Loop through the worksheets and each worksheet's list objects
    ' to find the name of the worksheet that contains the list object
    ' that the pivot table uses as its source data:
    Dim WS As Worksheet
    For Each WS In WB.Worksheets
        ' Loop through the ListObjects in each workshet:
        For Each LO In WS.ListObjects
            ' If the ListObject's name is the name of the pivot table's soure data,
            ' set the LOWS to be the worksheet that contains the list object:
            If LO.Name = PT.SourceData Then
                Set LOWS = WB.Worksheets(LO.Parent.Name)
            End If
        Next LO
    Next WS

    Debug.Print LOWS.Name

End Sub

Maybe someone knows a more direct way.

于 2014-01-23T23:43:02.260 回答
4

本答案所述,将范围转换为表格:

Sub CreateTable()
    ActiveSheet.ListObjects.Add(xlSrcRange, Range("$B$1:$D$16"), , xlYes).Name = _
        "Table1"
        'No go in 2003
    ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleLight2"
End Sub
于 2013-08-03T08:13:55.573 回答
3

添加第三个选项。@AndrewD 的第二个选项的“速记”版本。

  1. SheetObject.ListObjects("表名")
  2. Application.Range("TableName").ListObject
  3. [表名].ListObject

是的,括号参考中没有引号。

于 2020-01-11T04:46:43.210 回答