Not quite clear to me, but if you want to avoid the message box, something like this ...Hi
How I can Delete The Sheets through Code ? With msg box confermation ?
Thanks
If vbYes = MsgBox("Delete " & ActiveSheet.Name & " ?",vbYesNo + vbCritical) Then
On Error Resume Next
Application.DisplayAlerts = False
ActiveSheet.Delete
Application.DisplayAlerts = True
On Error GoTo 0
End If
Sub DelSh()
Dim ws As Worksheet, i As Integer, Response As VbMsgBoxResult
For Each ws In ThisWorkbook.Worksheets
If Worksheets.Count > 1 Then
Response = MsgBox("Delete sheet " & ws.Name & " ?", vbYesNo + vbQuestion)
If Response = vbYes Then
Application.DisplayAlerts = False
ws.Delete
Application.DisplayAlerts = True
End If
End If
Next ws
End Sub
Private Sub UserForm_Initialize()
Dim i As Long
For i = 1 To Sheets.Count
Me.ListBox1.AddItem Sheets(i).Name
Next
End Sub
Private Sub CommandButton1_Click()
With Me.ListBox
If .ListIndex = -1 Then Exit Sub
If vbYes = MsgBox("Delete " & .Value & " ?",vbYesNo + vbCritical) Then
On Error Resume Next
Application.DisplayAlerts = False
Sheets(.Value).Delete
Application.DisplayAlerts = True
If Err <> 0 Then MsgBox "You need at least one sheet"
On Error GoTo 0
End If
End With
End Sub
Correction:
should beRich (BB code):With Me.ListBox
Rich (BB code):With Me.ListBox1
Response = MsgBox("Delete sheet " & ws.Name & " ?", vbYesNo + vbQuestion)
Or perhaps
Code:Sub DelSh() Dim ws As Worksheet, i As Integer, Response As VbMsgBoxResult For Each ws In ThisWorkbook.Worksheets If Worksheets.Count > 1 Then Response = MsgBox("Delete sheet " & ws.Name & " ?", vbYesNo + vbQuestion) If Response = vbYes Then Application.DisplayAlerts = False ws.Delete Application.DisplayAlerts = True End If End If Next ws End Sub