Point 1: There are few things we need to keep in mind here, since we want to delete the specified sheets, the operation need to be carried out if all/any of them are present. Along with that, if we check against SheetName.Contains property, it might delete uninteded sheets based on the letterβs presence, which is indeed a conflict.
Point 2: Letβs say your file has only 1 Sheet i.e, A. In that case you cannot delete the sheet, as a workbook must contain atleast one visible worksheet.
Thus, you can try the following VBA code to achieve the desired result:
Sub sampleMacro()
Dim ws As Worksheet
Application.DisplayAlerts = False
If SheetExists("A") Then
Sheets("A").Delete
End If
If SheetExists("B") Then
Sheets("B").Delete
End If
If SheetExists("C") Then
Sheets("C").Delete
End If
If SheetExists("D") Then
Sheets("D").Delete
End If
Application.DisplayAlerts = True
For Each ws In ThisWorkbook.Worksheets
If ws.Name = "A" Or ws.Name = "B" Or ws.Name = "C" Or ws.Name = "D" Then
MsgBox "Failed to delete one or more sheets."
Exit Sub
End If
Next
MsgBox "Sheets deleted successfully."
End Sub
Function SheetExists(sheetName As String) As Boolean
SheetExists = False
On Error Resume Next
SheetExists = Not Sheets(sheetName) Is Nothing
End Function
Save this code in a text file, use Invoke VBA activity, provide the input file path, and mention the entry method as sampleMacro