View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Bob Phillips[_6_] Bob Phillips[_6_] is offline
external usenet poster
 
Posts: 11,272
Default Help in Excel VBA Programing - How to check an Excel Worksheet?

Here is a simple function to do it


'-----------------------------------------------------------------
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

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"wmcray " wrote in message
...
I am very new to Excel VBA programming.

I want to write a sample command to check if there is an existing Excel
worksheet, say for example called "Report".

If there is an existing worksheet, then it should be deleted.
Otherwise, a new worksheet called "Report" should be created.

Here is the program code. Can anyone please help me with the first
line???

If ???? (there is a worksheet called "report") Then
Sheets("Report").Delete
Else
Sheets.Add
ActiveSheet.Name = "Report"
End If

Thanks!!


---
Message posted from http://www.ExcelForum.com/