6

I have several tables in an excel sheet. Each having unique table Name. I want to know if a table which has a name "Table123" exist or not in the current sheet.

Could some one help me on this?

Thanks Jeevan

4
  • just loop table/shape colection colection and check if any table/shape has desired name. Commented Jun 29, 2016 at 10:39
  • How do I do that? How do I loop a ListObject? Commented Jun 29, 2016 at 10:42
  • Why is this question voted down with "-2" ? Commented Jun 29, 2016 at 15:00
  • @Jeevan its common practice on SO to downvote questions that dont abide by the community guidelines for asking questions -stackoverflow.com/help/how-to-ask. In your case, the lack of attempted code has resulted in some people downvoting your question. I still chose to answer because the solution was quite short, but that is the cause of the downvotes Commented Jun 30, 2016 at 7:03

7 Answers 7

13
TableExists = False
On Error GoTo Skip
If ActiveSheet.ListObjects("Table123").Name = "Table123" Then TableExists = True
Skip:
    On Error GoTo 0

This code will work and avoid loops and errors

Sign up to request clarification or add additional context in comments.

12 Comments

For very large data sets, this method will be much faster than looping through every shape on the sheet and will return exactly the same result
Getting run-time error for this. Its caused because i dont have shape Table123 on sheet, only table. So you need to change it to ListObjects and also when table isnt on sheet, you will get error. Not false
It is erroring out in this line "If Not ActiveSheet.Shapes.Range(Array("Table123")) Is Nothing Then" saying "The item with the specified name wasn't found"
It should not error out for me also that time that table was present.
error is Run-time error 1004 The item with the specified name wasnt found. (and i updated my previous coment)
|
7

Here is an alternative function:

Function TableExistsOnSheet(ws As Worksheet, sTableName As String) As Boolean
    TableExistsOnSheet = ws.Evaluate("ISREF(" & sTableName & ")")
End Function

2 Comments

This will return true for Names listed in the Name Manager and any strings that are Cell references. Since not all Names are tables, it might be better to use structured reference. i.e. "ISREF(" & sTableName & "[[#All]])" should evaluate false on non-table references. **I'm not sure if there are any edge cases.
Voting up because it was very helpful. A few comments though: 1. This doesn't answer OPs question if he's only looking for a table. If the name matches a non-table name then it could cause issues. 2. Instead of passing the worksheet through as a parameter, you could just pass sTableName and do TableExistsOnSheet = ThisWorkbook.ActiveSheet.Evaluate("ISREF(" & sTableName & ")") 3. Your function name TableExistsOnSheetis a misnomer as it will return true even if it is not on that sheet. It only has to be somewhere in the workbook.
5

You can list shape collection and compare names like this

Sub callTableExists()

    MsgBox tableExists("Table1", "Shapes")

End Sub

Function TableExists(tableName As String, sheetName As String) As Boolean

    Dim targetSheet As Worksheet
    Set targetSheet = Worksheets(sheetName)

    Dim tbl As ListObject

    With targetSheet
        For Each tbl In .ListObjects
            If tbl.Name = tableName Then TableExists = True
        Next tbl
    End With

End Function

Comments

0

Another option, using a bit lazy approach with error catching:

Public Sub TestMe()

    If TableExists("Table1243", ActiveSheet) Then
        MsgBox "Table Exists"
    Else
        MsgBox "Nope!"
    End If

End Sub    

Public Function TableExists(tableName As String, ws As Worksheet) As Boolean

    On Error GoTo TableExists_Error
    If ws.ListObjects(tableName).Name = vbNullString Then
    End If
    TableExists = True

    On Error GoTo 0
    Exit Function

TableExists_Error:    
    TableExists = False    

End Function

Comments

0

Try this, use err to get data table status information also, consider testing the data table on an inactive sheet.

Sub Test_TableNameExists()
    TableNm = "Table00"
    MsgOutput = TableNm & vbTab & TableNameExists(TableNm)
End Sub


Private Function TableNameExists(nname) As Boolean '#Table #Exist
'Returns TRUE if the data table name exists
'Can test table on inactive sheet
Dim x As Object
On Error Resume Next
'use Range(nname).Parent to get data table sheet name. 
'So the function can test data table on inactive sheet.

Set x = Range(nname).Parent.ListObjects(nname)
If Err = 0 Then TableNameExists = True _
    Else TableNameExists = False

End Function

Comments

0

Without the use of GoTo, which is a lot more powerfull than appropriate.

Set TableExists = False

On Error Resume Next
If ActiveSheet.ListObjects("Table123").Name = "Table123" Then Set TableExists = True

Be aware that this applies to a single line, thus requiring the line continuation symbol _ to keep larger statements readable.

Comments

0

Lots of answers that work here. IMO though error trapping has its place, I dislike the notion of purposely generating and redirecting internal errors to programmatically return a value.

Here is a function that will find if a table exists in a Workbook or specific worksheet (if provided) without relying on error trapping. Admittedly, it's a little more complicated, but it does exactly what it should and nothing more.

Function TableExists(sTableName As String, Optional mySheet As Worksheet) As Boolean

    Dim myListObject As ListObject
    Dim numsheets As Long
    Dim s, lo As Long
    Dim found As Boolean
    Dim passed As Boolean
    
    passed = (mySheet Is Nothing)
    
    If passed Then
    
        s = 1
        numsheets = Application.ActiveWorkbook.Sheets.Count
        
    Else
    
        s = mySheet.Index
        numsheets = 1
        
    End If

    found = False
    
    While ((Not found) And (s <= numsheets)) Or passed
        
        If (numsheets > 1) Or (Not passed) Then
            
            Set mySheet = Application.ActiveWorkbook.Worksheets(s)
        
        End If
        
        lo = 1
        While (Not found) And (lo <= mySheet.ListObjects.Count)
        
            Set myListObject = mySheet.ListObjects(lo)
        
            found = (sTableName = myListObject.Name)
            
            lo = lo + 1
        
        Wend
        
        passed = False 'Avoid any infinite loops from or clause
        s = s + 1
    
    Wend

    TableExists = found

End Function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.