Checking whether a worksheet exist or not

  • Thread starter Thread starter ajitpalsingh200
  • Start date Start date
A

ajitpalsingh200

hie,

How do i check whether the worksheet by the name of "results" exist o
not?

thankx in advanc
 
For Each sht In Worksheets
if sht.Name = "results" then
msgbox "results exists"
exit function
end if
Next


- Manges
 
try this:

Sub CheckSheet()
On Error GoTo erHandler
If ThisWorkbook.Sheets("results").Index > 0 Then
MsgBox "Sheet1 exists"
Exit Sub
End If
erHandler:
MsgBox "Sheet1 does not exist"
End Sub

KL
 
sorry, here is the correct version:

Sub CheckSheet()
On Error GoTo erHandler
If ThisWorkbook.Sheets("results").Index > 0 Then MsgBox "sheet Results
exists"
Exit Sub
erHandler:
MsgBox "sheet Results does not exist"
End Sub
 
ANother option

Check with

If SheetExists("results") Then
...

'-----------------------------------------------------------------
Function SheetExists(Sh As String, _
Optional wb As Workbook) As Boolean
'-----------------------------------------------------------------
Dim oWs As Worksheet
If wb Is Nothing Then Set wb = ActiveWorkbook
On Error Resume Next
SheetExists = CBool(Not wb.Worksheets(Sh) Is Nothing)
On Error GoTo 0
End Function



--

HTH

RP
(remove nothere from the email address if mailing direct)
 
Back
Top