Delete Sheets

hOSAMnOOR

New Member
Joined
Dec 21, 2008
Messages
12
Hi

How I can Delete The Sheets through Code ? With msg box confermation ?

Thanks
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Hi

How I can Delete The Sheets through Code ? With msg box confermation ?

Thanks
Not quite clear to me, but if you want to avoid the message box, something like this ...

Application.DisplayAlerts = False
Sheets("Sheet1").Delete
Application.DisplayAlerts = True
 
Upvote 0
Something like this
Code:
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
 
Upvote 0
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
 
Upvote 0
Thanks For All

I need code with msgbox include with list box with sheets name , then i can choose the sheet name

but sheet 1 cannot be delete because it's the main page

thanks again for all
 
Upvote 0
Create a userform with

1 x ListBox (ListBox1)
1 x CommandButton (CommandButton1)

to the form module
Code:
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
 
Upvote 0
Thanks jindon<SCRIPT type=text/javascript> vbmenu_register("postmenu_1785091", true); </SCRIPT>

Very Nice Code
 
Upvote 0
Correction:
Rich (BB code):
With Me.ListBox
should be
Rich (BB code):
With Me.ListBox1
 
Upvote 0
I have attempted to adjust this code to only provide option to delete the active sheet, and not provide an option for all sheets in the workbook. I am unable to make adjustments without causing an error.

All I aim to achieve is to delete the ActiveSheet and the prompt confirmation to include the worksheet name...i.e.

Code:
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
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,039
Messages
6,122,799
Members
449,095
Latest member
m_smith_solihull

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top