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. LS says:

    Hi
    I want to write a macro that counts the following
    Number of cells with formulae
    Number of Rows with data/formulae
    Number of Columns with data/formulae

    Please let me know if there is a way of doing it in Excel VBA

  2. Pingback: Number of Cells/Rows/Columns With Formula | Excel VBA Macro Tutorial and Examples

  3. Poer says:

    Hi LS, please find your answer here.

  4. Pingback: Bail Bonds Los Angeles

  5. Pingback: debt consolidation companies

  6. Pingback: de reverse mortgage

  7. Pingback: debt consolidation company

  8. Pingback: debt consolidation loans

  9. Pingback: consolidation loans

  10. Pingback: reverse mortgage costs

  11. Pingback: air treatment systems

  12. Pingback: The Number One Article on Project Management Gantt Charts

  13. Pingback: debt collection rate

  14. Pingback: Wedding photographer services calgary

  15. Pingback: collection agency rates

  16. Pingback: verona relais

  17. Pingback: buy usb bear

  18. Pingback: collection agency

  19. Pingback: Club de fans de Selena Gomez

  20. Pingback: collection agency newsletter

  21. Pingback: Sammelkarten

  22. Pingback: Printable Laundry Detergent Coupons 2012

  23. Pingback: online free credit report

  24. Pingback: wedding photographer calgary

  25. Pingback: tatuaggi maori

  26. Pingback: Pearl Izumi Running Shoe Deals

  27. Pingback: meer info

  28. Pingback: pick up lines for guys to use on girls

  29. Pingback: cushion cut egagement ring

  30. Pingback: Erotiek Groothandel

  31. Pingback: Kutjes

  32. Pingback: landscape gardeners north london

  33. Pingback: free microsoft points generator

  34. Pingback: Radiologist schools in Maryland

  35. Pingback: Custom CRM

  36. Pingback: Outdoor Signs

  37. Pingback: walmart credit card

  38. Pingback: bermuda homme

  39. Pingback: ????? paypal ????????????

  40. Pingback: phlebotomy training

  41. Pingback: SEO Services Plans

  42. Pingback: hobby lobby

  43. Pingback: SEO Services

  44. Pingback: home depot coupons

  45. Pingback: chota bheem games

  46. Pingback: Aeropostale coupons

  47. Pingback: Bed Bath and Beyond Canada

  48. Pingback: amazon coupon code

  49. Pingback: ????? ?????????????

  50. Pingback: Kung Fu Panda Games

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>