
Originally Posted by
Admin
Found an interesting challenge
here
Challenge is, delete all worksheets except one. I guess the one sheet would be the first one. Even if it's not the first one we can move to first.
Here is my attempt.
Code:
Sub kTest()
Dim i As Long, a
i = Worksheets.Count
a = Application.Transpose(Evaluate("Row(2:" & i & ")"))
Worksheets(a).Select
Application.DisplayAlerts = 0
Worksheets(a).Delete
Application.DisplayAlerts = 1
End Sub
You can reduce all that down to 3 lines of code...
Code:
Sub DeleteAllButSheet1()
Application.DisplayAlerts = False
Worksheets(Application.Transpose(Evaluate("Row(2:" & Worksheets.Count & ")"))).Delete
Application.DisplayAlerts = True
End Sub
If this code will always run alone, that is, it will not be called from within another VB code procedure, then you can reduce the macro down to 2 lines of code...
Code:
Sub DeleteAllButSheet1()
Application.DisplayAlerts = False
Worksheets(Application.Transpose(Evaluate("Row(2:" & Worksheets.Count & ")"))).Delete
End Sub
Bookmarks