Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel VBA - Get name of table based on cell address

Tags:

excel

vba

I am using Excel and am looking to get the name of the table based on a cell address (ex A3), this cell will not move. How would I go about stating this in Excel's VBA?

My plan is to have code that will copy data validations from a row of one table on my Maintenance tab to a single table on each tab of my workbook (minus my "TOC" and "data" tabs). Each tab is a copy of a "TEMPLATE" worksheet (minus the "TOC", "data", & the "TEMPLATE (Maint.)" worksheets). Worksheets "data", "TEMPLATE", and "TEMPLATE (Maint.)" may or may not be hidden.

The code I have in my "Copy_Data_Validations" sub is as follows:

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
like image 556
David S. Avatar asked Mar 14 '16 19:03

David S.


2 Answers

Attempting to get the name of a ListObject for any cell will cause an error if that cell is not a part of a table.

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
like image 172
PeterT Avatar answered Nov 15 '22 06:11

PeterT


The original question was a bit ambiguous, thus the answer was extended to address all related use-cases.

One possible alternative is to use the Worksheet Formula shown below entered in any Worksheet Cell (for e.g. $A$3) and then refer it from Excel VBA macro:

Listing 1. Get Excel Worksheet Name using Cell Formula

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

The Formula essentially extracts the Worksheet Name from the Workbook full path.

Alternatively, you can achieve this in VBA provided that you pass the Range object referring that cell in Worksheet, like in the following demo sample:

Listing 2. Test Sub to get Excel Worksheet and Table Names for Cell "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

Listing 3. Function to get a Worksheet Name for a Cell

'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

And, in it's simplest form, the Worksheet Name could be obtained using the following statement (replacing that one in the Function shown in Listing 3):

Listing 4. Alternative method to get Parent Worksheet Name for Cell/Range object

GetWorksheetName = CellRange.Parent.Name

In order to get the Table Name for the specified Worksheet Cell refer to the code snippet shown in the following Listing 5:

Listing 5. Get Table Name for Worksheet Cell

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

Hope this may help.

like image 34
Alexander Bell Avatar answered Nov 15 '22 05:11

Alexander Bell