Consulting

Results 1 to 2 of 2

Thread: Solved: Sheets Problem

  1. #1
    VBAX Regular
    Joined
    Apr 2007
    Posts
    9
    Location

    Solved: Sheets Problem

    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.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    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
    [/vba]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •