Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Please I have been working on creating a new sheet using VBA in excel workbook.. please kindly help as i have the script to do that but doesnt know how to work through it.


VB
Private Sub Workbook_NewSheet(ByVal Sh As Object)
Sub TestSheetCreate()
    Dim mySheetName As String, mySheetNameTest As String
    mySheetName = "Sheet7"

    On Error Resume Next
    mySheetNameTest = Worksheets(mySheetName).Name
    If Err.Number = 0 Then
        MsgBox "The sheet named ''" & mySheetName & "'' DOES exist in this workbook."
    Else
        Err.Clear
        Worksheets.Add.Name = mySheetName
        MsgBox "The sheet named ''" & mySheetName & "'' did not exist in this workbook but it has been created now."
    End If
End Sub
Posted
Comments
Richard MacCutchan 4-Aug-14 12:21pm    
What happens when you try it? You could also check this quite easily by recording a macro and creating a new shhet. Stop recording and edit the macro to see what commands are run.
Maciej Los 6-Oct-14 14:16pm    
It's impossible to add nested procedure, like this:
Private Sub Workbook_NewSheet(ByVal Sh As Object)
Sub TestSheetCreate()

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900