Pregunta

He agregado un TableStyle al libro de Excel con la declaración:

ActiveWorkbook.TableStyles.Add("PivotTable SS")

Puedo borrarlo usando:

ActiveWorkbook.TableStyles("PivotTable SS").Delete

¿Cómo puedo verificar mediante programación si ya existe antes de decidir si eliminar o no?

Actualmente estoy recorriendo todos los estilos de tabla y haciendo una eliminación selectiva:

    For Each ts In ActiveWorkbook.TableStyles
        If ts.Name = "PivotTable Style 1" Then
            ts.Delete
        End If
    Next ts

Sin embargo, esto lleva mucho tiempo. ¿Cómo puedo verificar la existencia de la tabla dinámica y eliminarla sin hacer un bucle?

Gracias :)

¿Fue útil?

Solución

Puedes intentar asignar el estilo a una variable. Si la variable es Nothing, entonces el estilo no existe. Si el estilo no existe e intenta asignar la variable, recibirá un mensaje de error, por lo que deberá suspender temporalmente el manejo de errores.

Sub DeleteAStyle()

    Dim ts As TableStyle

    On Error Resume Next
    Set ts = ActiveWorkbook.TableStyles("PivotTable Style 1")
    On Error GoTo MyUsualErrorHandler

    If Not ts Is Nothing Then
        ts.Delete
    End If

End Sub
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top