Problem
A worksheet within a workbook shall be deleted via VBA in Excel.
Ein Arbeitsblatt soll in einer Arbeitsmappe gelöscht werden.
Approach / Ansatz
- Create a new SUB method with the worksheet name as parameter / Definition einer neuen Sub-Methode mit Übergabe des Namen des zu löschenden Arbeitsblattes
- Iterate all worksheets / über alle Arbeitsblätter iterieren
- Check the name of the current worksheet / Namen der Worksheets prüfen
- When the name matches the SUB paramter, the worksheet will get deleted via .Delete method / Wenn der Name des übergebenen Parameters entspricht, wird das worksheet gelöscht mit der .Delete Methode
- Check the name of the current worksheet / Namen der Worksheets prüfen
Solution / Lösung
Sub DeleteWorksheet(worksheetName As String)
Dim ws As Worksheet, wb As Workbook
Set wb = ActiveWorkbook
Application.DisplayAlerts = False
For Each ws In wb.Worksheets
If ws.Name = worksheetName Then
ws.Delete
Exit For
End If
Next
Application.DisplayAlerts = True
End Sub