+ Reply to Thread
Results 1 to 4 of 4

Macro to delete worksheets

  1. #1
    Registered User
    Join Date
    04-20-2013
    Location
    san francisco
    MS-Off Ver
    Excel 2012
    Posts
    6

    Cool Macro to delete worksheets

    I am trying to create a macro that will delete all worksheets except for worksheets I do not want deleted. I want all worksheets to be deleted except for worksheets ABC and DEF. I am having trouble and would be greatful for any help. Thank you.

    The code I am using is as follows:

    Sub DeleteSheets()
    Dim ws As Worksheet

    For Each ws In ActiveWorkbook.Worksheets

    If ws.Name <> "ABC" Then
    Application.DisplayAlerts = False
    If ws.Name <> "DEF" Then
    Application.DisplayAlerts = False
    ws.Delete
    Application.DisplayAlerts = True
    End If
    Next ws

    End Sub
    Last edited by mac_us_27; 04-20-2013 at 10:51 PM.

  2. #2
    Forum Guru benishiryo's Avatar
    Join Date
    03-25-2011
    Location
    Singapore
    MS-Off Ver
    Excel 2013
    Posts
    5,156

    Re: Macro to delete worksheets

    hi mac_us_27, welcome to the forum. Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code in [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    then try this:
    Please Login or Register  to view this content.

    Thanks, if you have clicked on the * and added our rep.

    If you're satisfied with the answer, click Thread Tools above your first post, select "Mark your thread as Solved".

    "Contentment is not the fulfillment of what you want, but the realization of what you already have."


    Tips & Tutorials I Compiled | How to Get Quick & Good Answers

  3. #3
    Registered User
    Join Date
    04-20-2013
    Location
    san francisco
    MS-Off Ver
    Excel 2012
    Posts
    6

    Re: Macro to delete worksheets

    Thank for this, but one more question if I wanted to have five worksheets excluded...would it look like this?

    Sub DeleteSheets()
    Dim ws As Worksheet

    For Each ws In ActiveWorkbook.Worksheets

    If ws.Name <> "ABC" And ws.Name <> "DEF" And ws.Name <> "EKF" And ws.Name <> "DFC" And ws.Name <> "QRS" And ws.Name <> "FOB" Then
    Application.DisplayAlerts = False
    ws.Delete
    Application.DisplayAlerts = True
    End If
    Next ws

    End Sub

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Macro to delete worksheets

    One way
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1