Home Excel Powerpoint Word Outlook Error Codes

VBA Useful Codes

Welcome to the World of VBA
Home > VBAUsefulCodes

Excel Macro to delete a worksheet


Use the following code to delete the existing sheet in a workbook

Sub deleteWorksheet()
    Dim wb As Workbook
    Dim sh As Worksheet

    Dim newSheetName As String
    newSheetName = "March"

    Dim fPath As String

    fPath = "C:\myworkbook.xlsx"
    Set wb = workbooks.Open(Filename:=fPath)
    ' delete first worksheet
    Set sh = wb.Worksheets(1)    
    ' also instead you can use Set sh = wb.Worksheets("shtname")
    ' Following statemet will launch an excel built in
    ' delete confirmation popup message.
    ' once you confirm it manually then this sheet would be deleted
    Application.DisplayAlerts = False ' to disable to delete confirmation popup
    sh.Delete ' now delete the sheet
    Application.DisplayAlerts = True  ' to disable to delete confirmation popup

End Sub

Subscribe

Enter your e-mail below and get notified on the latest blog posts.




Tags

VBA Outlook KPI Excel Alt+F11 Dashboards Macros Recording Graphs Automation Developer WaterFall Powerpoint Charts Pivot Tables Forecast Charts


Follow Us