By using t2do.com you agree to our cookie policy, We and our partners operate globally and use cookies, for multiple purposes

T2Do.com

 


CreateSheet



Function CreateSheet(sh As String)
'Version: 1.002
'Purpose: To Create a sheet for passed name and delete the sheet if it already exists
On Error Resume Next
CreateSheet = ""
Worksheets(sh).Visible = True
On Error Resume Next
Worksheets(sh).Select
If Err = 9 Or Err = -2147352565 Then
    'nothing to do sheet doesn't exist
ElseIf Err = 0 Then
    'Delete the sheet
    If ActiveWindow.SelectedSheets.Count > 1 Then
         MsgBox "Something went wrong. Multiple sheets were selected."
    Else
        Application.DisplayAlerts = False
        
        ActiveWindow.SelectedSheets.Delete '@ this is worrysome
        Application.DisplayAlerts = True
    End If
    
Else
    CreateSheet = "Unexpected error during CreateSheet = " & Err & " " & Error
    
End If
On Error Resume Next

Sheets.Add.Name = sh
If ActiveSheet.Name <> sh Then
    'There may be a problem with Excel
    CreateSheet = "Excel is behaving Strangely. Added sheet but not added. Usually do to resource issue"
    Exit Function
End If

    Sheets(sh).Move After:=Sheets(Sheets.Count)

End Function