Excel VBA - 根据单元格地址获取表的名称

时间:2016-03-14 19:01:59

标签: excel vba excel-vba

我正在使用Excel并希望根据单元格地址(例如A3)获取表格的名称,此单元格不会移动。我将如何在Excel的VBA中说明这一点?

我的计划是将代码从我的维护选项卡上的一个表的行复制数据验证到我工作簿的每个选项卡上的单个表(减去我的“TOC”和“数据”选项卡)。每个选项卡都是“模板”工作表的副本(减去“TOC”,“数据”和“TEMPLATE(Maint。)”工作表)。工作表“data”,“TEMPLATE”和“TEMPLATE(Maint。)”可能会隐藏也可能不会隐藏。

我的“Copy_Data_Validations”子代码中的代码如下:

Dim TotalSheets As Integer
Dim p As Integer
Dim iAnswer As VbMsgBoxResult

With Application
    .DisplayAlerts = False
    .ScreenUpdating = False
End With

'
' Move sheet "TOC" to the begining of the workbook.
'
Sheets("TOC").Move Before:=Sheets(1)
'
' Move sheet "data" to be the second sheet in the workbook.
'
Sheets("data").Move Before:=Sheets(2)

iAnswer = MsgBox("You are about to copy data validations!", vbOKCancel + vbExclamation _
+ vbDefaultButton2 + vbMsgBoxSetForeground, "Copying Data Valadations")
For TotalSheets = 1 To Sheets.Count
    For p = 3 To Sheets.Count - 2
'
' If the answer is Yes, then copy data validations from "TEMPLATE (Maint.) to all other.
' sheets minus the "TOC" sheet and the "data" sheet.
'
        If iAnswer = vbYes Then
            If UCase$(Sheets(p).Name) <> "TOC" And UCase$(Sheets(p).Name) <> "data" Then

                ' This chunk of code should copy only the data validations
                ' of "Table1_1" (A4:AO4) from the maintenance tab to all
                ' rows of a single table on each worksheet (minus the
                ' "TOC", "data", & the "TEMPLATE (Maint.)" worksheets.
                ' This is the section of code I am looking for unless
                ' someone has something better they can come up with.

                Selection.PasteSpecial Paste:=xlPasteValidation, _
                Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            End If
'
' If the answer is Cancel, then cancels.
'
        ElseIf iAnswer = vbCancel Then
        ' Add an exit here.
        End If

With Application
    .DisplayAlerts = True
    .ScreenUpdating = True
End With

2 个答案:

答案 0 :(得分:7)

尝试为任何单元格获取ListObject的名称将导致错误,如果该单元格不是表格的一部分。

Option Explicit

Function CellInTable(thisCell As Range) As String
    Dim tableName As String
    tableName = ""
    On Error Resume Next
    tableName = thisCell.ListObject.Name
    CellInTable = tableName
End Function

答案 1 :(得分:2)

最初的问题有点含糊不清,因此扩展答案以解决所有相关的用例。

一种可能的替代方法是使用在Worksheet Formula(例如Worksheet)中输入的Cell $A$3,然后从Excel中引用它VBA宏:

清单1.使用单元格公式获取Excel 工作表 名称

=MID(CELL("filename",A3),FIND("]",CELL("filename",A3))+1,255)

公式实质上从Worksheet Name完整路径中提取Workbook

或者,您可以在VBA中实现此目的,前提是您在Range中传递引用该单元格的Worksheet对象,如下面的演示示例所示:

清单2.测试Sub以获取Excel 工作表 名称获取单元格“A3”

Option Explicit

'Cell "A3" under the test
Sub GetWorksheetAndTableName()
    Dim myCell As Range
    Set myCell = Range("$A$3")
    Debug.Print "Worksheet Name: " & GetWorksheetName(myCell)
    Debug.Print "Table Name: " & GetTableName(myCell)
End Sub

清单3.为单元格获取工作表 名称的功能

'get Worksheet Name from Range object
Function GetWorksheetName(CellRange As Range) As String
    On Error Resume Next
    GetWorksheetName = Split(Split(CellRange.Address(External:=True), "]")(1), "!")(0)
End Function

并且,以最简单的形式,可以使用以下语句获取Worksheet Name(在清单3所示的函数中替换该语句):

清单4.为Cell / Range对象获取Parent 工作表 名称的替代方法

GetWorksheetName = CellRange.Parent.Name

要获取指定Worksheet Cell的表名,请参阅以下清单5中显示的代码段:

清单5.获取工作表单元格的表名

Function GetTableName(CellRange As Range) As String
    If (CellRange.ListObject Is Nothing) Then
        GetTableName = ""
    Else
        GetTableName = CellRange.ListObject.Name
    End If
End Function

希望这可能会有所帮助。