是否有可能在 Excel VBA 中引用一个命名表?
假设这可能是...
Sheets("Sheet1").Table("A_Table").Select
我已经看到一些提到的表是一个列表对象,但我不知道这是否是同样的事情。

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 中很难使用!

Excel 中的“表”实际上称为 ListObject。
引用表的“正确”方法是从其工作表中获取其 ListObject,即SheetObject.ListObjects(ListObjectName)
。
如果要在不使用工作表的情况下引用表,可以使用 hackApplication.Range(ListObjectName).ListObject
。
注意:此黑客攻击依赖于 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 信息here。

此外,定义引用对象的变量也很方便。例如,
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
你可能会立即发现它的优势。
除了上面的,你可以这样做(其中“YourListObjectName”是你的表的名称):
Dim LO As ListObject
Set LO = ActiveSheet.ListObjects("YourListObjectName")
但我认为,只有当你想引用活动表上的列表对象时才有效。
我找到了你的问题,因为我想在一个工作表上引用一个列表对象 (一个表),另一个工作表上的数据表指的是。由于列表对象是 Worksheets 集合的一部分,你必须知道列表对象所在的工作表的名称才能引用它。
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
也许有人知道更直接的方法。
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(85条)