Although (unfortunately) such a method is not available, we can create our own function to test this.
We hope that the code below meets your needs.
Edit1: Added delete statement ...
Sub test() If CheckSheet(Sheets(3).Name) then Application.DisplayAlerts = False Sheets(Sheets(3).Name).Delete Application.DisplayAlerts = True End If End Sub
A solution for which I would like ...
Function CheckSheet(ByVal sSheetName As String) As Boolean Dim oSheet As Excel.Worksheet Dim bReturn As Boolean For Each oSheet In ActiveWorkbook.Sheets If oSheet.Name = sSheetName Then bReturn = True Exit For End If Next oSheet CheckSheet = bReturn End Function
Alternatively, if you don't mind using code that actively raises errors (which is not recommended using common coding rules), you can use this Spartan Programming wannabe 'code below ...
Function CheckSheet(ByVal sSheetName As String) As Boolean Dim oSheet As Excel.Worksheet Dim bReturn As Boolean For Each oSheet In ActiveWorkbook.Sheets If oSheet.Name = sSheetName Then bReturn = True Exit For End If Next oSheet CheckSheet = bReturn End Function Function CheckSheet(ByVal sSheetName As String) As Boolean On Error Resume Next Dim oSheet As Excel.Worksheet Set oSheet = ActiveWorkbook.Sheets(sSheetName) CheckSheet = IIf(oSheet Is Nothing, False, True) End Function
Tiago Cardoso Jul 27 '11 at 2:26 2011-07-27 02:26
source share