+ Reply to Thread
Results 1 to 4 of 4

VBA Delete Multiple Worksheets

Hybrid View

  1. #1
    Registered User
    Join Date
    03-17-2017
    Location
    Illinois
    MS-Off Ver
    2013
    Posts
    38

    VBA Delete Multiple Worksheets

    I have a workbook that users add sheets to, and save and the workbook just get's junked up. I know you can use VBA to delete a sheet, just by simply executing
    For Each ws In ActiveWorkbook.Worksheets
      ws.Delete
    Next ws
    But that is slow! What I am after is to delete ANY sheet that is not named "Master Data" or "Holding Pattern" in the workbook.

  2. #2
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: VBA Delete Multiple Worksheets

    Not sure if this will provide any speed increase or not, but the syntax I always use to exclude a worksheet or two when needing to delete is
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Name <> "Holding Pattern" Or ws.Name <> "Master Data" Then
            ws.Delete
        End If
    Next ws
    I think the sluggishness comes into play when your workbook has a large number of sheets in it, as this syntax above iterates over each worksheet individually.

  3. #3
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: VBA Delete Multiple Worksheets

    Hi Jo15765
    You can add
    Application.ScreenUpdating = False 
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Name <> "Holding Pattern" Or ws.Name <> "Master Data" Then
            ws.Delete
        End If
    Next ws
    Application.ScreenUpdating = True
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: VBA Delete Multiple Worksheets

    Try this.
        Application.DisplayAlerts = False
    
        For Each ws In ActiveWorkbook.Worksheets
            If ws.Name <> "Holding Pattern" And ws.Name <> "Master Data" Then
                ws.Delete
            End If
        Next ws
    
        Application.DisplayAlerts = True
    If posting code please use code tags, see here.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] VBA to delete the same row in multiple worksheets
    By Faridwahidi in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-09-2015, 10:39 PM
  2. [SOLVED] delete rows from multiple worksheets
    By dckrause in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-06-2005, 03:05 AM
  3. delete rows from multiple worksheets
    By dckrause in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-06-2005, 02:05 AM
  4. delete rows from multiple worksheets
    By dckrause in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-06-2005, 01:05 AM
  5. delete rows from multiple worksheets
    By dckrause in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-05-2005, 11:05 PM
  6. [SOLVED] delete rows from multiple worksheets
    By dckrause in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-05-2005, 10:05 PM
  7. [SOLVED] Can you delete multiple worksheets with one command?
    By pebel in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-17-2005, 10:06 PM

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