Please can I have a VBA module that will do the following. Delete all sheets in the…
- User
Please can I have a VBA module that will do the following. Delete all sheets in the workbook that the name begins with Task
- Assistant
Certainly! Below is a VBA module that you can use to delete all sheets in the active workbook whose names begin with "Task". Please ensure to back up your workbook before executing this code, as it will permanently delete sheets.
Here's the VBA code you can use:
Sub DeleteTaskSheets() Dim ws As Worksheet Dim sheetsToDelete As Collection Dim sheetName As String ' Initialize a collection to hold the names of sheets to delete Set sheetsToDelete = New Collection ' Loop through all worksheets in the active workbook On Error Resume Next ' Ignore errors temporarily For Each ws In ThisWorkbook.Worksheets If Left(ws.Name, 4) = "Task" Then sheetsToDelete.Add ws.Name End If Next ws On Error GoTo 0 ' Re-enable error handling ' Delete the sheets stored in the collection For Each sheetName In sheetsToDelete Application.DisplayAlerts = False ' Turn off alerts for deleting sheets ThisWorkbook.Worksheets(sheetName).Delete Application.DisplayAlerts = True ' Turn alerts back on Next sheetName MsgBox "Sheets with names beginning with 'Task' have been deleted.", vbInformation End Sub - User
Please can I have a Yes No message before deletion asking "This action will delete all Taskddmmyy sheet" line break "Do you want to continue"
- Assistant
Certainly! You can add a message box that prompts the user for confirmation before proceeding with the deletion of the sheets. Here's the modified version of the previous code that includes a confirmation prompt:
- User
I am getting an Error: For Each control varaible must be Variant or Object on the line - For Each sheetName In sheetsToDelete, pertaining to sheetName
Sub DeleteTaskSheets() Dim ws As Worksheet Dim sheetsToDelete As Collection Dim sheetName As String Dim confirmResponse As VbMsgBoxResult
' Initialize a collection to hold the names of sheets to delete Set sheetsToDelete = New Collection ' Loop through all worksheets in the active workbook On Error Resume Next ' Ignore errors temporarily For Each ws In ThisWorkbook.Worksheets If Left(ws.Name, 4) = "Task" Then sheetsToDelete.Add ws.Name End If Next ws On Error GoTo 0 ' Re-enable error handling ' Check if there are any sheets to delete If sheetsToDelete.Count = 0 Then MsgBox "No sheets with names beginning with 'Task' found.", vbInformation Exit Sub End If ' Prompt for confirmation confirmResponse = MsgBox("This action will delete the following sheets:" & vbCrLf & _ Join(sheetsToDelete.ToArray(), vbCrLf) & vbCrLf & _ "Do you want to continue?", vbYesNo + vbQuestion, "Confirm Deletion") ' If the user clicks Yes, delete the sheets If confirmResponse = vbYes Then Application.DisplayAlerts = False ' Turn off alerts for deleting sheets For Each sheetName In sheetsToDelete ThisWorkbook.Worksheets(sheetName).Delete Next sheetName Application.DisplayAlerts = True ' Turn alerts back on MsgBox "Sheets with names beginning with 'Task' have been deleted.", vbInformation Else MsgBox "Deletion cancelled.", vbInformation End If - Assistant