Results 1 to 6 of 6

Thread: Delete worksheets without loop

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    15
    Quote Originally Posted by Admin View Post
    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
    Last edited by Admin; 08-14-2012 at 10:05 PM.

Similar Threads

  1. Loop to two columns and Concatenate values
    By ivandgreat in forum Excel Help
    Replies: 15
    Last Post: 04-14-2013, 08:20 PM
  2. Loop Through And Delete Multiple File Types In A Folder
    By Excel Fox in forum Excel and VBA Tips and Tricks
    Replies: 0
    Last Post: 03-30-2013, 04:47 PM
  3. Loop through a folder and find word
    By k0st4din in forum Excel Help
    Replies: 7
    Last Post: 12-08-2012, 02:22 PM
  4. Speed up Loop VBA
    By PcMax in forum Excel Help
    Replies: 15
    Last Post: 04-09-2012, 04:20 PM
  5. Loop in Array
    By stanleydgromjr in forum Excel Help
    Replies: 5
    Last Post: 07-28-2011, 05:06 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •