Smartening up of VBA

SuperFerret

Well-known Member
Joined
Mar 2, 2009
Messages
515
Hello,

I have managed to put together code from this site, and added in some extra bits that I needed.

Basically what the macro should be doing:
1) Open the Workbook to sheet MACROS if Macros are not enabled - keeping all other sheets hidden.
2) If Macros enabled, show the Discontinued and ALL LIVE. sheets - not MACROS sheet
3) Before saving/closing - unfilter all columns on Discontinued sheet
4) Reset to MACROS sheet

This works, but I'm not anywhere near a genius on VBA yet so I don't know if this can be neatened/cleaned up any, and if so how?

Can anyone offer me advice?

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    With Application
        .EnableCancelKey = xlDisabled
        .ScreenUpdating = False
        Call HideSheets
        .ScreenUpdating = True
        .EnableCancelKey = xlInterrupt
    End With
    ThisWorkbook.Save
    ThisWorkbook.Saved = True
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim WS As Worksheet
    For Each WS In Worksheets
         WS.AutoFilterMode = False
    Next WS
    With Application
        .EnableCancelKey = xlDisabled
        .ScreenUpdating = False
        Call HideSheets
        .ScreenUpdating = True
        .EnableCancelKey = xlInterrupt
    End With
 
    ThisWorkbook.Save
    ThisWorkbook.Saved = True
End Sub
Private Sub Workbook_Open()
 
    'Unhide all worksheets
 
            With Application
                .EnableCancelKey = xlDisabled
                .ScreenUpdating = False
 
                Call UnhideSheets
 
                .ScreenUpdating = True
                .EnableCancelKey = xlInterrupt
            End With
 
End Sub
Private Sub HideSheets()
Dim Sheet As Object
    With Sheets("MACROS")
        .Visible = xlSheetVisible
            For Each Sheet In Sheets
                If Not Sheet.Name = "MACROS" Then
                    Sheet.Visible = xlSheetVeryHidden
                End If
            Next
        Set Sheet = Nothing
    End With
End Sub
Private Sub UnhideSheets()
    Sheets("Discontinued").Visible = xlSheetVisible
    Sheets("MACROS").Visible = xlSheetVeryHidden
    Sheets("ALL LIVE.").Visible = xlSheetVisible
 
End Sub
 
Presuming the other visible sheet does not need any filtering left on, have you tried just tacking it in before making the sheets hidden?
Rich (BB code):
For Each wksWorksheet In ThisWorkbook.Worksheets
    If Not wksWorksheet.CodeName = "shtForceEnable" Then
        wksWorksheet.AutoFilterMode = False
        wksWorksheet.Visible = xlSheetVeryHidden
    End If
Next
 
Upvote 0

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

Forum statistics

Threads
1,225,156
Messages
6,183,224
Members
453,152
Latest member
ChrisMd

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