Hiding Worksheet on save event

DavidB

Active Member
Joined
Oct 21, 2002
Messages
253
Office Version
  1. 365
  2. 2013
Platform
  1. Windows
Hi there

I have written the following bit of code triggered off the workbook_before save event.


Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
' Hide the UPS worksheet on the Workbook_BeforeSave event
If Sheets("UPS").Visible <> xlVeryHidden Then
Debug.Print "Sheets(""UPS"").Visible = "; Sheets("UPS").Visible
Sheets("UPS").Visible = xlVeryHidden
' For an unknown reason the previous line does not execute when the before_save event is triggered by a program.
'Debug.Print "Sheets(""UPS"").Visible = "; Sheets("UPS").Visible
End If
End Sub
Sub SaveIt()
ActiveWorkbook.Save
End Sub


The code is in the Thisworkbook object and works perfectly from the file save menu path or the save icon.

My problem is when another macro saves the workbook the
Sheets("UPS").Visible = xlVeryHidden
line does not execute.

Is this by design or have I found a bug? I am using Excel 2003.

Regards David
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hi Andrew

This code is an extract from a much larger application, I have distilled it down to the problem code only. This distillation is contained in a stand-alone workbook and only that workbook is open so there should be no confusion about which workbook to operate on.

I have stepped through the code and the highlighting visits that line but does not appear to execute it. The debug.print lines before and after when uncommented perform as expected.

This morning I have tried two more minor changes with the same result.

1) Moved the SaveIt code from the module to the workbook object.

2) Changed the problem line to
Sheets("UPS").Visible = xlVeryHidden: Debug.Print "Hello"
Both sub portions of this line are visited during the line by line execution and the debug.Print returns the expected "Hello"

I am a little confused.

Regards David
 
Upvote 0
Thanks for your help Andrew

Odd behaviour to say the least.

I have come up with an interim solution. I just have to ensure that all prior to using the line "ActiveWorkbook.Save " I must ensure that the WorkSheet is very hidden.

I do not beleive that the code should behave like this and I don't like having to remember such work arounds. I am fortunate that the calls will only occur in a very limited number of places.

Regards DavidB
 
Upvote 0
Hi DavidB,

This is a know issue / problem. Silly but it is.
http://support.microsoft.com/kb/898511/en-us

I allways use the following method:
In ThisWorkbook:
Code:
Option Explicit

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Run "Clear_The_Stuff"
End Sub
In a Module called "Clearing"
Code:
Option Explicit

Sub Clear_The_Stuff()
    '... other code ...
    If Sheets("UPS").Visible = True Then
        Sheets("UPS").Visible = xlVeryHidden
    End If
End Sub
And in a Code Modules:
Code:
Option Explicit

Sub YourCode()
    '... other code ...
    Run "Clear_The_Stuff"
    ThisWorkbook.Save
End Sub

In this way you have covered both ways of Saving the file.

PS:
I prefer to use special Modules for special Code so I can find them easely.
Naming Modules (referring to what they are for) and so grouping Macro's is much easier then Module1 till Module 7.

Succes. :wink:
 
Upvote 0
Thanks Oele

I don't feel so bad now :biggrin: knowing that I am not alone.

Regards DavidB
 
Upvote 0

Forum statistics

Threads
1,215,473
Messages
6,125,018
Members
449,203
Latest member
tungnmqn90

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