Search TeachExcel.com
TeachExcel.com
TE
Teach Excel MS Office Tutorials Excel Consulting Services Excel Forum
Excel Video Tutorials Excel Tips Free Excel Macros Excel Help Resources Contact TeachExcel
Video Tutorials
  • Free Macros
  • Excel Help Directory
  • Excel 2007 Resources
  • Keyboard Shortcuts
  • Excel Forum
  • Contact/About

Macros
Excel Tutorials For Macros

Delete Hidden Worksheets


Bookmark and Share

This macro will delete all hidden worksheets within a workbook. When you run this macro a warning window will pop up for each hidden sheet making sure that you want to delete it. This is a precaution which Microsoft Excel takes and not the actually macro itself. Though, this is good because it makes sure you think twice before deleting something that could have necessary or valuable information contained within it.
Where to install the macro:  Module

Delete Hidden Worksheets

Sub DeleteHiddenWorksheets()

'This Macro will remove all hidden worksheets from a workbook.
'There will be a warning window that pops up to make sure you want
'to do this just in case there is other information that is needed
'within those hidden sheets.

i = 1
While i <= Worksheets.Count
If Not Worksheets(i).Visible Then
Worksheets(i).Delete
Else
i = i + 1
End If
Wend

End Sub


Bookmark and Share


How to Install the Macro
  1. Select and copy the text from within the grey box above.

  2. Open the Microsoft Excel file in which you would like the Macro to function.

  3. Press "Alt + F11" - This will open the Visual Basic Editor - Works for all Excel Versions.  Or For other ways to get there, Click Here.

      For Excel Versions Prior to Excel 2007
      Go to Tools > Macros > Visual Basic Editor

      For Excel 2007
      Go to Office Button > Excel Options > Popular > Click Show Developer tab in the Ribbon. Then go to the Developer tab on the ribbon menu and on the far left Click Visual Basic

  4. On the new window that opens up, go to the left side where the vertical pane is located. Locate your Excel file; it will be called VBAProject (YOUR FILE'S NAME HERE) and click this.

  5. If the Macro goes in a Module, Click Here, otherwise continue to Step 8.

    1. Go to the menu at the top of the window and click Insert > Module
    2. Another window should have opened within the Visual Basic Editor's window. Within this new window, paste the macro code. Make sure to paste the code underneath the last line of anything else that is in the window.
    3. Go to Step 8.

  6. If the Macro goes in the Workbook or ThisWorkbook, Click Here, otherwise continue to Step 8.

    1. Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
    2. Then, at the bottom of the list that appears, double-click the ThisWorkbook text.
    3. A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
    4. Go to Step 8.

  7. If the Macro goes in the Worksheet Code, Click Here, otherwise continue to Step 8.

    1. Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
    2. Within the list that appears you will see every worksheet that is in that excel file. They will be listed as such: Sheet1(NAME OF SHEET HERE) and under that will be Sheet2(NAME OF SHEET HERE). Select the sheet in which you want the macro to run and double-click that sheet.
    3. A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
    4. Repeat steps b and c for every sheet you want the macro to work in. Putting the macro in one sheet will not enable it for any other sheets in the workbook.
    5. Go to Step 8.

  8. Close the Microsoft Visual Basic Editor window and save the Excel file. When you close the Visual Basic Editor window, the regular Excel window will not close.

  9. You are now ready to run the macro.



Similar Helpful Excel Resources

Macro To Delete Hidden Worksheets? - Excel

View Content
Any ideas? It would be great of I could delete all but one of the hidden worksheets. It would be perfect if I could tell the macro not to delete one of the hidden worksheets (called "Test O2 and CO2"), but if that is too difficult, then just one to delete all the hidden sheets would be fine.

Any help would be much appreciated on this one,

James

Delete Certain Ranges Across Mulitiple Worksheets Whether Hidden Or Not - Excel

View Content
Hi,

I would like to delete certain ranges across mutliple worksheets, for example range F7:F17, P7:P17, Q7:17, across mutilple worksheets whether they are hidden sheets or not hidden, what I would also like is a message box that asks for confirmation of whether they wish to proceed to delete (would it be possible to include the range in the message box, what is about to be deleted - then a confirmation message saying delete has been actioned or cancelled - is it also possible to put a password on the before running the macro if the user selects yes to delete.

Many thanks

How To Delete All Hidden Worksheets When Workbook Saved - Excel

View Content
I have working code to unhide specific worksheets from a large number of hidden worksheets and would like to be able to delete all the hidden sheets when the workbook is saved or maybe have a button to delete all the hidden (un-needed) worksheets beside the selection drop-down menu, so that only the selected sheets are saved.

Geoff

How Do I Detect Hidden Worksheets Or Hidden Data On A Worksheet? - Excel

View Content
I have a workbook with one worksheet which appears to be empty, however the
saved file is 3.5Mb. I am at a loss as to where all that content is?


Macros To Delete Hidden Sheets And Hidden Rows - Excel

View Content
Hi there,

I have built a monster spreadsheet, with the aim for it to cover all possible scenarios at my job. It basically involves a sheet for each type of test I may do. Some days it may need 1 sheet, others it may need 10. These all get populated into a single "Report" sheet.

What I am pulling my hair out over is trying to create 2 macros, which would be run one after the other.

The first would be to delete all hidden sheets in the workbook.

The second would be to delete all hidden rows, in the "Report" sheet only, and at the same time find any ",!#REF!" occurences that may have been generated by deleting rows, and replacing them with nothing. i.e. some cells may average 10 cells, deleting some rows will introduce say 5 valid cells, and 5 which have been deleted, leaving the ",!#REF!".

Can anyone out there help me out with these 2 macros please?

Thanks, James

Macro To Show Or Hide Worksheets From A List Of Visible Or Hidden Worksheets - Excel

View Content
Hi everyone,

My first post. I've been using this site for years and it is awesome.

Here is the situation. We have a workbook with a start page. Users answer questions on this sheet and based on the answer, various worksheets can be either made visible or hidden. Everything is working fine with this. Feedback from our end users is that they also want the ability to show/hide worksheets regardless of how questions are answered on the start page - like an override function.

I have started work on a new tab with a list of all the applicable worksheets within the workbook down column A with a dropdown in column B with the option to show/hide. This code is triggered by the "Private Sub Worksheet_Change(ByVal Target As Range)" line. The end user can choose which tabs to show/hide based on the situation. The problem is that column B is currently blank, so I was searching for code that can assist with populating the visible state of tabs based on column A.

I was going to continue down this path, but came across this nice piece of code that may work and looks cooler, but I do not understand it enough to modify. Now this code lists all sheets and prints out the ones you checkmark. What I would like is it to list all visible/hidden (but NOT very hidden) sheets and already have the ones visible checked, then the user can check or remove check from sheets to show/hide.

http://www.ozgrid.com/forum/showthread.php?t=19572

FromWill Riley

VB:

 
Sub SelectSheets() 
    Dim i As Integer 
    Dim TopPos As Integer 
    Dim SheetCount As Integer 
    Dim PrintDlg As DialogSheet 
    Dim CurrentSheet As Worksheet 
    Dim cb As CheckBox 
    Application.ScreenUpdating = False 
     
     ' Check For protected workbook
    If ActiveWorkbook.ProtectStructure Then 
        MsgBox "Workbook is protected.", vbCritical 
        Exit Sub 
    End If 
     
     ' Add a temporary dialog sheet
    Set CurrentSheet = ActiveSheet 
    Set PrintDlg = ActiveWorkbook.DialogSheets.Add 
     
    SheetCount = 0 
     
     ' Add the checkboxes
    TopPos = 40 
    For i = 1 To ActiveWorkbook.Worksheets.Count 
        Set CurrentSheet = ActiveWorkbook.Worksheets(i) 
         ' Skip empty sheets And hidden sheets
        If Application.CountA(CurrentSheet.Cells) <> 0 Then 
            SheetCount = SheetCount + 1 
            PrintDlg.CheckBoxes.Add 78, TopPos, 150, 16.5 
            PrintDlg.CheckBoxes(SheetCount).Text = _ 
            CurrentSheet.Name 
            TopPos = TopPos + 13 
        End If 
    Next i 
     
     ' Move the OK And Cancel buttons
    PrintDlg.Buttons.Left = 240 
     
     ' Set dialog height, width, And caption
    With PrintDlg.DialogFrame 
        .Height = Application.Max _ 
        (68, PrintDlg.DialogFrame.Top + TopPos - 34) 
        .Width = 230 
        .Caption = "Select sheets to print" 
    End With 
     
     ' Change tab order of OK And Cancel buttons
     ' so the 1st option button will have the focus
    PrintDlg.Buttons("Button 2").BringToFront 
    PrintDlg.Buttons("Button 3").BringToFront 
     
     ' Display the dialog box
    CurrentSheet.Activate 
    Application.ScreenUpdating = True 
    If SheetCount <> 0 Then 
        If PrintDlg.Show Then 
            For Each cb In PrintDlg.CheckBoxes 
                If cb.Value = xlOn Then 
                    If Not Worksheets(cb.Caption).Visible Then 
                        With Worksheets(cb.Caption) 
                            .Visible = True 
                            .Activate 
                            .PrintOut 
                            .Visible = False 
                        End With 
                    Else 
                        With Worksheets(cb.Caption) 
                            .Activate 
                            .PrintOut 
                        End With 
                    End If 
                     ' ActiveSheet.PrintPreview 'for debugging
                End If 
            Next cb 
        End If 
    Else 
        MsgBox "All worksheets are empty." 
    End If 
     
     ' Delete temporary dialog sheet (without a warning)
    Application.DisplayAlerts = False 
    PrintDlg.Delete 
     
     ' Reactivate original sheet
    CurrentSheet.Activate 
End Sub 


If you like these VB formatting tags please consider sponsoring me in support of injured Royal Marines



Using Vba With Hidden Worksheets Or Protected Worksheets - Excel

View Content
Hi,

I have a file with VBA but don't want the my client to see some of my worksheets or don't want them to revise any of my stuff in this Excel file. I would like to either hide my working worksheets or protected the worksheets. However, I've tried both but the VBA shows as Debug. How can I hide the sheets or protected them and the VBA still works.

Thanks.

Worksheets Hidden - Excel

View Content
Hello

The sheets in my worksheet seem to have disappeared off the bottom of my worksheet so I can only access 1 sheet

Tried some things in advanced options but it seems to have ticked show all windows in the taskbar in display

Could it be something to do with moving files from one laptop to another with different XL versions?

Thanks
Nick

Hidden Worksheets - Excel

View Content
I am having an issue with a workbook on the open in excel 2007. I open the workbook and the sheets are not visible. I then click view, arrange all, tiled, and OK. The worksheet appears and I save it, however the next time I reopen the workbook, the sheet is hidden again.

Has anyone seen this before and/or have any ideas on how to fix this?

Thanks

Delete Hidden - Excel

View Content
Hi,

I have the following marco to delete all hidden sheets in a workbook.

When this is ran you are prompted to confirm you would like to delete each sheet individually. Can someone tell me how this can be done automatically without the prompt?

I have tried enableevents= false events but this doesn't work.

Thanks,

Ali





Code:

Sub deletehidden()

Dim a As Integer


a = 1

While a <= Worksheets.Count

If Not Worksheets(a).Visible Then

Worksheets(a).Delete
Else
a = a + 1
End If
Wend

End Sub




Random Tutorials
Make a Thermometer Style Chart in Excel
(Intermediate)
Introduction to Making Formulas in Excel
(Easy)
Absolute and Relative Cell References
   - & INDIRECT() Function Introduction

(Easy)
Assign Keyboard Shortcuts to Macros
(Easy)
Formatting Worksheets for Printing in Excel
(Easy)
How to 'Protect' a Worksheet and Workbook
(Easy)
Submit Inquiry Here
  • Prices From $10
Name:*
E-mail:*
Request:*
The macro(s) on this page will be sent with the request.
Contact | Privacy Policy | Disclaimer
Copyright© 2012 TeachExcel.com