New Excel Forum

This forum has been moved to TeachExcel.com

Ask all future questions in the New Excel Forum.

ExcelKey

Macro to delete sheets listed in colum of master sheet

Macros, VBA, Excel Automation, etc.

Macro to delete sheets listed in colum of master sheet

Postby kellyjones » Mon Jan 09, 2012 10:24 pm

I have a master sheet in a workbook that has a list of worksheets (not all the worksheets in the workbook) that I would like to delete.

I've seen several codes that do this, but I can't get them to work--i'm new to Macros.

Any help would be appreciated.

So in B10 through B80 there is a list of worksheets. I'd like a Maro that would delete these

Thanks

Kelly
  • 0

kellyjones
Rookie
 
Posts: 1
Joined: Jan 9, 2012
Reputation: 0

Re: Macro to delete sheets listed in colum of master sheet

Postby Sisyphus » Tue Jan 10, 2012 12:54 am

Hi,

The following code wasn't tested:
Code: Select all
Public Sub DeleteSheets()
    Dim R as Long
    For R = 10 to 80
        Application.DisplayAlerts = False
        Worksheets(Cstr(ActiveSheet.Cells(R, 2).Value)).Delete
        Application.DisplayAlerts = True
    Next R
End Sub

It also has no safeguards. If the name in column B isn't a valid name the macro will crash. If it crashes the Option "DisplayAlerts" will be caught switched off. You can switch it on by typing the command in the Immediate Window of the VB Editor or by restarting Excel.

Install this macro in The ThisWorkbook or any Code module in your workbook where the list is. The sheets to be deleted must be in the same workbook. Call the macro by selecting it from the list of available macros you get from Tools/Macro/Macros and pressing Run. If you choose to run it from the VB Editor instead, make sure that the ActiveSheet is the one with the list in it. The ActiveSheet is the worksheet that you last saw before switching to the VB Editor.

I strongly suggest that you try it out by setting the For ... Next loop to a single worksheet, like
For R = 10 to 10
  • 0

Have a great day! :D

Sisyphus
I do this for "honour and country" - much less of the latter, actually.
If I helped you, award points, plenty of them.
If I bored you, deduct points for being too long-winded. (I know, :lol)
Sisyphus
Former Moderator
 
Posts: 4454
Joined: Dec 7, 2011
Location: Shanghai
Reputation: 203
Excel Version: 2010


Return to Macros and VBA Questions

Who is online

Users browsing this forum: No registered users and 248 guests