Consulting

Results 1 to 6 of 6

Thread: determine if a sheet exist

  1. #1
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location

    determine if a sheet exist

    hello
    how do i determine if a sheet name "cc" exist in a workbook?.
    namely if it is then do so & so if it's not then create one.
    thanks
    moshe

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]
    If Not SheetExists("cc") Then
    Worksheets.Add.Name = "cc"
    End If

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

  3. #3
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location
    hello
    whats wrong with
    [VBA]
    Sub targill()
    Application.ScreenUpdating = False
    If Not SheetExists("cc") Then
    Worksheets.Add.Name = "cc"
    Exit Sub
    Else
    Sheets("cc").Delete
    Dim newsheet
    Worksheets("projects").Range("A1:C4000").Copy
    Set newsheet = Worksheets.Add
    newsheet.Range("A1").PasteSpecial paste:=xlValues
    ActiveSheet.Name = "cc"
    Application.ScreenUpdating = True
    End If
    End Sub

    [/VBA]
    moshe

  4. #4
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Could you tell us what the problem is?

  5. #5
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location
    Is this what you're after ?



    [VBA]Sub Test()
    Dim ws As Worksheet
    Application.ScreenUpdating = False
    For Each ws In Worksheets
    If ws.Name = "CC" Then ws.UsedRange.Clear
    Next ws
    On Error Resume Next
    If IsError(Sheets("CC").Select) Then Sheets.Add
    ActiveSheet.Name = "CC"
    On Error GoTo 0
    Worksheets("projects").Range("A1:C4000").Copy
    Range("A1").PasteSpecial Paste:=xlValues
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    End Sub[/VBA]

  6. #6
    I always used something like:
    [VBA]On Error Resume Next
    Worksheets("Main").Select
    If Err <> 0 Then MsgBox "Sheet does not exist."
    On Error GoTo 0
    [/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
  •