View Single Post
 
Old 07-06-2012, 07:23 AM
Colin Legg's Avatar
Colin Legg Colin Legg is offline Windows 7 32bit Office 2010 32bit
Expert
 
Join Date: Jan 2011
Location: UK
Posts: 369
Colin Legg will become famous soon enough
Default

Hi,

Here's one way:

Code:
Sub Main()
    
    Dim wstNew As Worksheet
    
    If WorksheetExists(WhichWorkbook:=ThisWorkbook, WorksheetName:="My New Worksheet") Then
        ThisWorkbook.Activate
        ThisWorkbook.Worksheets("My New Worksheet").Select
    Else
        With ThisWorkbook.Worksheets
            Set wstNew = .Add(After:=.Item(.Count))
            wstNew.Name = "My New Worksheet"
        End With
    End If
    
End Sub

Function WorksheetExists(ByVal WhichWorkbook As Workbook, ByRef WorksheetName As String) As Boolean
    On Error Resume Next
        
    WorksheetExists = Not WhichWorkbook.Worksheets(WorksheetName) Is Nothing
End Function
Reply With Quote