VBA to unhide sheet then re hide

thedeadzeds

Active Member
Joined
Aug 16, 2011
Messages
451
Office Version
  1. 365
Platform
  1. Windows
Hi Guys,

Is it possible to use a use a macro to take you to a specific worksheet and then once you have click off this worksheet it rehides it?

Thanks
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
To hide a sheet when you click off it, add this to its code module:-

Code:
Private Sub Worksheet_Deactivate()
[I]Sheet1[/I].Visible = xlSheetHidden
End Sub

This is for Sheet1: change the name if your sheet is called something else or use its name, e.g. Sheets("MainSheet").

The tricky bit is how to select the sheet you want the macro to take you to in the first place. Do you need a menu of sheets to choose from, a pop-up box asking you for a sheet name, or do you want to be taken there automatically when 'something happens'?
 
Upvote 0
The following macro will unhide Sheet2, activate it and select cell A1.

Code:
Sub UseSheetThenHideIt()
    With Sheets("Sheet2")    [COLOR=#b22222]'change name to suit[/COLOR]
        .Visible = xlSheetVisible
        .Activate
        .Range("A1").select
    End With
End Sub

The macro could be attached to a button placed on the referring sheet or a button on the ribbon or a shortcut key sequence, etc.

Then if the following code is inserted into the 'Worksheet' (as opposed to 'General') section of the Sheet2 MS Excel Object (get there from the Project Explorer)

Code:
Private Sub Worksheet_Deactivate()
    If Me.Name <> "Sheet2" Then Me.Name = "Sheet2"  [COLOR=#b22222]'this ensures the macro does not break if someone tries to alter the sheet name[/COLOR]
    Me.Visible = xlSheetHidden    [COLOR=#b22222]'or xlSheetVeryHidden if you really don't want folk to know its there[/COLOR]
End Sub

Note the comment about xlSheetVeryHidden. Using that means Sheet2 will not be visible when right clicking another tab and selecting unhide. Clearly, change sheet names to suit your spreadsheet.

This works until the sheet name is changed by one of your users (hence the xlSheetVeryHidden tip which also needs to be supplemented by some protection - already in the code above) or you (no special advice on that one :)).
 
Upvote 0

Forum statistics

Threads
1,224,747
Messages
6,180,719
Members
452,995
Latest member
isldboy

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