macro-delete all sheets except

P

puiuluipui

Hi, i need a macro to delete all sheets except 4 or 5 sheets.
Can this be done?
Thanks!
 
J

Jacob Skaria

Mention the sheets not to be deleted in the string variable comma separated
without any spaces.. and try the macro
strSheet = "Sheet1,Sheet2"

Sub Macro()
Dim strSheet As String
strSheet = "Sheet1,Sheet2"
For Each sh In Worksheets
If InStr(1, "," & strSheet & ",", "," & sh.Name & ",", _
vbTextCompare) = 0 Then sh.Delete
Next
End Sub

If this post helps click Yes
 
P

puiuluipui

Hi Jacob, it's working, but it ask me everytime if i really want to delete.
Can this code be modify to display a message "did you saved previous month?"
and then, if i click ok, to delete all sheets without asking anithing else?
If i click cancel, to stop the macro , so i can save previous month.
Can this be done?
Thanks!
 
D

Don Guillett

Sub Macro()

Dim strSheet As String
x = InputBox("did", vbOKCancel)
If x = OK Then 'MsgBox "hi"

strSheet = "Sheet1,Sheet2"
application.displayalerts=false
For Each sh In Worksheets
If InStr(1, "," & strSheet & ",", "," & sh.Name & ",", _
vbTextCompare) = 0 Then sh.Delete
Next
application.displayalerts=true
end if
End Sub
 
P

puiuluipui

Hi Don, it's ok, but i need a simple msgbox with ok and cancel. Now i have a
msgbox with input message
Can this be done?
Thanks!
 
L

Luke M

Modifying Don's coding...

Sub Macro()

Dim strSheet As String
x = MsgBox("Did you save previous month?", vbOKCancel)
If x = vbCancel Then Exit Sub

strSheet = "Sheet1,Sheet2"
application.displayalerts=false
For Each sh In Worksheets
If InStr(1, "," & strSheet & ",", "," & sh.Name & ",", _
vbTextCompare) = 0 Then sh.Delete
Next
application.displayalerts=true
end if
End Sub
 
P

puiuluipui

Thanks you all!

Luke M said:
Modifying Don's coding...

Sub Macro()

Dim strSheet As String
x = MsgBox("Did you save previous month?", vbOKCancel)
If x = vbCancel Then Exit Sub

strSheet = "Sheet1,Sheet2"
application.displayalerts=false
For Each sh In Worksheets
If InStr(1, "," & strSheet & ",", "," & sh.Name & ",", _
vbTextCompare) = 0 Then sh.Delete
Next
application.displayalerts=true
end if
End Sub

--
Best Regards,

Luke M
*Remember to click "yes" if this post helped you!*
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top