Create New Excel Worksheet With VBA

The Excel VBA macro below will create a new Excel Worksheet called ‘RawData’ or we can use msgbox to ask for the Worksheet name if needed.

If there is already a Worksheet called RawData, user will be ask whether they want to use the old Worksheet and cancel new Worksheet creation, or delete the old Worksheet and continue creating a new blank Worksheet.

Sub CreateNewWorksheet()

    Dim oSheet As Worksheet, vRet As Variant

    On Error GoTo errHandler

    'creating a new excel worksheet called RawData
    Set oSheet = Worksheets.Add
    With oSheet
        .Name = "RawData"
        .Cells(1.1).Select
        .Activate
    End With
    Exit Sub

errHandler:

    'if error due to duplicate worksheet detected
    If Err.Number = 1004 Then
        'display an options to user
        vRet = MsgBox("Worksheet called 'RawData' is already exist, " & _
            "click yes to continue creating new Worksheet and delete the old one, " & _
            "or click no to go to the old worksheet.", _
            vbOKCancel, "Duplicate Worksheet")

        If vRet = vbOK Then
            'delete the old worksheet
            Application.DisplayAlerts = False
            Worksheets("RawData").Delete
            Application.DisplayAlerts = True

            'rename and activate the new worksheet
            With oSheet
                .Name = "RawData"
                .Cells(1.1).Select
                .Activate
            End With
        Else
            'cancel the operation, delete the new worksheet
            Application.DisplayAlerts = False
            oSheet.Delete
            Application.DisplayAlerts = True
            'activate the old worksheet
            Worksheets("RawData").Activate
        End If

    End If

End Sub
This entry was posted in Workbook and Worksheet and tagged , , , , , , , , , , , , , , . Bookmark the permalink.

768 Responses to Create New Excel Worksheet With VBA

  1. Pingback: official link

  2. Pingback: here's the site

  3. Pingback: effective search engine optimization

  4. Pingback: website

  5. Pingback: close window

  6. Pingback: link

  7. Pingback: urinary incontience

  8. Pingback: casque bluetooth

  9. Pingback: internet marketing service

  10. Pingback: close window

  11. Pingback: hosting ftp site

  12. Pingback: tall single men

  13. Pingback: HCG Drops Reviews

  14. Pingback: diabetic bags

  15. Pingback: duluth tree removal

  16. Pingback: sponsors

  17. Pingback: compare auto insurance quotes

  18. Pingback: commercial painters brisbane

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>