VBA help needed please

Dougie1

Board Regular
Joined
Jul 27, 2007
Messages
212
Hi all -

Using Excel 2007.

Can anyone help me modify the code below?

What it does on workbook close is hide all worksheets except Sheet1 & Sheet2.

I have recently added a Chart, but on close the code leaves it visible along with sheets 1 & 2.

I know it is something to do with the chart not being a worksheet, but am at a loss how to include it in this code. All help gratefully received.

TIA

D


Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim ws As Worksheet
    Application.Calculation = xlAutomatic
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> "Sheet1" And ws.Name <> "Sheet2" Then ws.Visible = xlSheetVeryHidden
    Next ws
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    
    Dim obj As Object, sh As Worksheet
    
    Application.Calculation = xlAutomatic
    
    For Each obj In Sheets
        If TypeName(obj) = "Worksheet" Then
            Set sh = obj
            If sh.Name <> "Sheet1" And sh.Name <> "Sheet2" Then
                sh.Visible = xlSheetVeryHidden
            End If
        End If
    Next

End Sub
 
Last edited:
Upvote 0
Sorry Sektor - it is NOT generating an error at that line - I missed some code while copying over.

I still have a problem though - the code is not hiding the chart sheet, leaving it still visible alongside sheets 1 & 2.

Any help appreciated
 
Upvote 0
Stealing Sektor's thunder this word version will do it:
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    
    Dim obj As Object, sh As Worksheet
    
    Application.Calculation = xlAutomatic
    
    For Each obj In Sheets
        If TypeName(obj) = "Worksheet" Then
            Set sh = obj
            If sh.Name <> "Sheet1" And sh.Name <> "Sheet2" Then
                sh.Visible = xlSheetVeryHidden
            End If
        End If
        
        If TypeName(obj) = "Chart" Then
            obj.Visible = xlSheetVeryHidden
        End If
    Next

End Sub

Or shorter less legible:
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    
    Dim obj As Object
    
    Application.Calculation = xlAutomatic
    
    For Each obj In Sheets
        If TypeName(obj) <> "Worksheet" Or (obj.Name <> "Sheet1" And obj.Name <> "Sheet2") Then
                obj.Visible = xlSheetVeryHidden
        End If
    Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,879
Members
452,948
Latest member
Dupuhini

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top