USER EMAIL : Excel macro to remove a sheet from workbook




EMAIL


I'm creating an application in excel and I'm stuck at a point where I need to remove a sheet from a workbook if the name is say "Engineering"


REPLY


Thank you for writing to Extreme Excel Solutions!!!
 

You need to add a loop that iterate through all the sheets.. Keep on checking the name of worksheet. Once you find the first match, exit out of loop after deleting.



Sub DeleteWorksheet()
 

For Each sh In Worksheets
    If sh.Name = "Engineering" Then
        Application.DisplayAlerts = False
        sh.Delete
        Application.DisplayAlerts = True
        Exit For
    End If
Next

End Sub



Note :  DisplayAlertsproperty is used to turn ON/OFF the alerts. Just to bypass the alert that pops in when you delete a sheet, we turned it OFF before deleting and reset it to ON after deleting.


Subscribe to our youtube channel and keep checking video tutorials to learn excel and vba.