Esta función requiere que el libro a comprobar esté abierto. Devuelve True si se encuentra la hoja y False si no.
SheetExists (SName,WBName)
Los argumentos son:
SName: El nombre de la hoja que se busca.
WBName: Opcional; el nombre del libro que contiene la hoja.
Function SheetExists(SName As String, Optional WB As Workbook) As Boolean
Dim WS As Worksheet
'Utiliza el libro activo por defecto
If WB Is Nothing Then
Set WB = ActiveWorkbook
End If
On Error Resume Next
sheetExist = CBool(Not WB.Sheets(SName) Is Nothing)
On Error GoTo 0
End Function
Este es el ejemplo de utilización:
Sub CheckForSheet()
Dim ShtExists As Boolean
ShtExists = ShtExists("Hoja9")
'Observa que solamente se ha pasado un parámetro; el nombre del libro es opcional
If ShtExists Then
MsgBox "La hoja existe"
Else
MsgBox "La hoja no existe"
End If
End Sub
Technorati Tags: 