English 中文(简体)
如何在不提及其表格的情况下参考一个结构化的表格
原标题:How to refer to a structured table without referring to its sheet in VBA

VBA 我知道,我可以这样提到一个结构化的表格:

  Set Tbl = Sheets("MySheetName").ListObjects("MyTblName")

然后是Tbl.XXX。 三十可以是:Name, .Range, 等等。

然而,我想在提及这份表格时不提及Sheet的名字,这样,如果表格名称有改动,宏观就不会消失。

这是可能的吗?

问题回答

After some research I found something that is not a perfect solution. You can make use of the Range function in VBA like this:

Set tbl = Range("TableName[#All]")

然而,这不是一个目标,而是范围。 你们也可以做其他参考:

结构化表格的体(不包括头盔)

Range("TableName")

名为“MyColumn”的栏目

Range("TableName[MyColumn]")

等等。

Then you call something like: tbl.ListObject to refer to the structured table where the range is found.

冷却的是,“工作范围”将永远用于“积极工作手册”,因此,你可以到“工作”栏,在《工作手册A》中开一个宏观,并且将继续使用《工作手册B》。

资料来源:

如果你想在一份身份不明的工作单上提一个表格(“功能”),你可以做些什么:

Set rng4= Range("tbl_Function[#All]")                        range of table
sh = rng4.Parent.Name                                        name of the worksheet where the table is placed
tbl = Sheets(sh).ListObjects("tbl_Function").Range.Value     take the whole table

希望会有所助益。 Piotr

如果您的桌子上,你可以这样说:

Set Tbl = ActiveSheet.ListObjects("MyTblName")

我在如何保护我的法典方面也存在同样的问题,即表格名称的变化。

I built on Piotr s excellent solution and developed these simplified procedures:-

Private Function connectTable(nmTbl As String) As Range
    Set connectTable = Sheets(Range(nmTbl).Parent.Name).ListObjects(nmTbl).DataBodyRange
End Function


Private Sub clearTable(tbl As Range)
    If tbl Is Nothing Then
        Exit Sub
          MsgBox "already cleared"
    ElseIf tbl.Rows.Count > 0 Then
         MsgBox "about to delete " & tbl.Rows.Count & " rows"
        tbl.Rows.Delete
    End If
End Sub

  Example how to call the above procedures
Public Sub genCashFlow()
Dim tabDetailCF As Range
Dim nmSheetTabDetailCF As String
Dim tabAsset As Range

    Set tabAsset = connectTable("tabAsset")
    Set tabDetailCF = connectTable("tabDetailCF")
    
    Call clearTable(tabDetailCF)

End Sub




相关问题
import of excel in SQL imports NULL lines

I have a stored procedure that imports differently formatted workbooks into a database table, does work on them then drops the table. Here is the populating query. SELECT IDENTITY(INT,1,1) AS ID ...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

Excel date to Unix timestamp

Does anyone know how to convert an Excel date to a correct Unix timestamp?

C# GemBox Excel Import Error

I am trying to import an excel file into a data table using GemBox and I keep getting this error: Invalid data value when extracting to DataTable at SourceRowIndex: 1, and SourceColumnIndex: 1. As ...

Importing from excel "applications" using SSIS

I am looking for any tips or resources on importing from excel into a SQL database, but specifically when the information is NOT in column and row format. I am currently doing some pre-development ...

热门标签