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.
On Error Resume Nextbefore the....Deleteline.