在 Excel VBA 中删除表名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45689580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 13:05:41  来源:igfitidea点击:

Delete table name in Excel VBA

excelvbaexcel-vba

提问by Tox

I would like to remove/delete the name of my table which is "Table2" using Excel VBA. I know how to remove the name by hand, but I cannot figure out how to set the name "Table2" as a name in VBA. I found other questions on this topic but those codes deleted all named ranges and I would like to remove just the table named "Table2".

我想使用 Excel VBA 删除/删除我的表的名称,即“Table2”。我知道如何手动删除名称,但我无法弄清楚如何将名称“Table2”设置为 VBA 中的名称。我发现了有关此主题的其他问题,但这些代码删除了所有命名范围,我只想删除名为“Table2”的表。

This is my code that is not working:

这是我的代码不起作用:

Sub Delete_Name_Table()

   Dim n As Name

   n = "Table2"

   n.Delete

End Sub

Anyone who knows how to set n correctly to "Table2"?

任何知道如何将 n 正确设置为“Table2”的人?

Thanks!

谢谢!

回答by Vityata

There is a good way to make a table to a range. Thus, the name is removed automatically, but the value stays. Like this:

有一个很好的方法可以将表格制作成一个范围。因此,名称会自动删除,但值会保留。像这样:

Public Sub TestMe()

    Dim tblN        As Object
    Dim rngRange    As Range

    For Each tblN In ActiveSheet.ListObjects
        Debug.Print tblN

        If tblN = "Tabelle16" Then
            tblN.Unlist
        End If

    Next tblN

End Sub

If you want to remove the data within as well, here is the code:

如果您还想删除其中的数据,这里是代码:

Public Sub TestMe()

    Dim n As String
    n = "Tabelle2"
    Range(n).Delete

End Sub

It will set nto a Table name and it will delete it.

它将设置n为表名称,并将删除它。

回答by Shai Rado

I hope I understood your post, and you want something like the code below:

我希望我理解你的帖子,并且你想要类似下面的代码:

Option Explicit

Sub Delete_Name_Table()

Dim Sht As Worksheet
Dim Tbl As ListObject

Set Sht = Worksheets("Sheet1") ' <-- change to your sheet that has the table

' set the Table Object
Set Tbl = Sht.ListObjects("Table2")
Tbl.Name = " " ' clear the name, you need to give it some kind of name

End Sub

回答by velblúd

You can backup data from table, delete table and restore data.

您可以从表中备份数据、删除表和恢复数据。

Sub test()
    Dim rng As Range
    Dim rngVals As Variant

    Set rng = YourSheet.ListObjects("Table2").Range
    rngVals = rng.Value

    YourSheet.ListObjects("Table2").Delete

    rng.Value = rngVals

    Set rng = Nothing
End Sub