PDA

View Full Version : Solved: Sheets Problem



jsc0624
04-12-2007, 05:15 AM
How to detect if the sheets is already existing? If the sheet is already existing the program should not create another one instead the program will use the existing sheet.

Bob Phillips
04-12-2007, 05:42 AM
Public Sub Test()
Dim sh As Worksheet
If SheetExists("Sheet99") Then
Set sh = Worksheets("Sheet99")
Else
Set sh = Worksheets.Add
sh.Name = "Sheet99"
End If
End Sub

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