3

I am trying to determine if a table exists, using VBA Excel 2007, and if it exists then delete it.

I am looping through an array of table names.

My code is below:

' Allocate
Dim lIndex                                      As Long

' Allocate table header values in array
Dim sTableNames(1 To Constants.lNumTables)      As String

' Populate array
sTableNames(1) = Constants.sTableNameKpiAllIncidents
sTableNames(2) = Constants.sTableNameSlaAllManualHelpdeskIncidents
sTableNames(3) = Constants.sTableNameSlaAllManualIncidents
sTableNames(4) = Constants.sTableNameKpiAllAutomaticIncidents

' Work in worksheet Statistics
With Worksheets(Constants.sSheetNameStatistics)

    ' Loop through all tables
    For lIndex = 1 To UBound(sTableNames)

        ' Check if table already exists
        If Not .ListObjects(sTableNames(lIndex)) Is Nothing Then

            ' Delete table
            .ListObjects(sTableNames(lIndex)).Delete

        End If

    Next

End With

My code works as long as these tables exist in my worksheet. I have also tried replacing the line

If Not .ListObjects(sTableNames(lIndex)) Is Nothing Then

with the line

 If .ListObjects(sTableNames(lIndex)).Count > 0 Then

but it still doesn't work.

Does anybody know a way to get this to work?

Any help would be appreciated.

4
  • 1
    What error do you get? The 'dirty' quick fix would be to add On Error Resume Next before the ....Delete line. Commented Apr 14, 2016 at 16:41
  • I get run-time error '9': Subscript out of range Commented Apr 14, 2016 at 16:43
  • 1
    It makes Excel not interrupt code execution in case it encounters an error. This is 'dirty' because all errors appear for a reason (in your case: the macro is trying to delete something that doesn't exist) and by jumping over it you may overlook other bugs. Commented Apr 14, 2016 at 16:50
  • Thank you! I've inserted the line before If Not .ListObjects(sTableNames(lIndex)) Is Nothing Then and it worked! Commented Apr 14, 2016 at 16:51

2 Answers 2

5

Error handling as nhee suggests is the right approach.

As a UDF the suggestion above would be quicker with:

Function TableExists(ws As Worksheet, tblNam As String) As Boolean
Dim oTbl As ListObject
On Error Resume Next
Set oTbl = ws.ListObjects(tblNam)
TableExists = Not oTbl Is Nothing
End Function
Sign up to request clarification or add additional context in comments.

Comments

2

The following UDF will return a boolean if a table exists

  Function TableExists(ws As Worksheet, tblNam As String) As Boolean
    Dim oTbl As ListObject
      For Each oTbl In ws.ListObjects
        If oTbl.Name = tblNam Then
            TableExists = True
            Exit Function
        End If
      Next oTbl
      TableExists = False
    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.