ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   macro-delete all sheets except (https://www.excelbanter.com/excel-discussion-misc-queries/245251-macro-delete-all-sheets-except.html)

puiuluipui

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

Jacob Skaria

macro-delete all sheets except
 
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
---------------
Jacob Skaria


"puiuluipui" wrote:

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


puiuluipui

macro-delete all sheets except
 
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!

"Jacob Skaria" wrote:

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
---------------
Jacob Skaria


"puiuluipui" wrote:

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


Don Guillett

macro-delete all sheets except
 
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


--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"puiuluipui" wrote in message
...
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!

"Jacob Skaria" wrote:

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
---------------
Jacob Skaria


"puiuluipui" wrote:

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



puiuluipui

macro-delete all sheets except
 
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!

"Don Guillett" wrote:

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


--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"puiuluipui" wrote in message
...
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!

"Jacob Skaria" wrote:

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
---------------
Jacob Skaria


"puiuluipui" wrote:

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




Luke M

macro-delete all sheets except
 
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!*


"puiuluipui" wrote:

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!

"Don Guillett" wrote:

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


--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"puiuluipui" wrote in message
...
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!

"Jacob Skaria" wrote:

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
---------------
Jacob Skaria


"puiuluipui" wrote:

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




puiuluipui

macro-delete all sheets except
 
Thanks you all!

"Luke M" wrote:

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!*


"puiuluipui" wrote:

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!

"Don Guillett" wrote:

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


--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"puiuluipui" wrote in message
...
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!

"Jacob Skaria" wrote:

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
---------------
Jacob Skaria


"puiuluipui" wrote:

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




All times are GMT +1. The time now is 06:36 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com