VBA: Hide 1 worksheet, Show 1 worksheet

lawi227

Board Regular
Joined
Mar 17, 2016
Messages
123
In cell F5 on worksheet "Instructions", I have a drop down menu. The user will either select "1" or "2". If the user selects "1", then I would like to hide the tab called "Org. 2 Calcs" and make sure the tab called "Org. 1 Calcs" is showing. If the user selects "2", then I would like "Org. 1 Calcs" AND "Org. 2 Calcs" to be showing.

Note: there are several other tabs in this workbook so the VBA coding cannot use and codes that "close all tabs".

Thanks!
 
Thank you! But can you make one minor edit? When "2" is selected, I need to hide Sheets("Org. 1 Calcs"), and make Sheets("Org. 2 Calcs") visible. The current code makes Sheets("Org. 1 Calcs") AND Sheets("Org. 2 Calcs") visible.

Thanks!

Then you could put this in the code module for the Instructions sheet.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Application.Intersect(Target, Range("F5")) Is Nothing Then
        With Range("F5")
            If .Value = 1 Or .Value = 2 Then Sheets("Org. 1 Calcs").Visible = xlSheetVisible
            Sheets("Org. 2 Calcs").Visible = IIf(.Value = "2", xlSheetVisible, xlSheetHidden)
        End With
    End If
End Sub
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
<style type="text/css">p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; background-color: #ffffff}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #011993; background-color: #ffffff}span.s1 {color: #011993}span.s2 {color: #000000}</style>Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Range("F5")) Is Nothing Then
With Range("F5")
If .Value = 1 Or .Value = 2 Then Sheets("LPA Tables & Graphs (1 org)").Visible = xlSheetVisible
Sheets("LPA Tables & Graphs (2 orgs)").Visible = IIf(.Value = "2", xlSheetVisible, xlSheetHidden)
Sheets("LPA Tables & Graphs (1 org)").Visible = xlSheetHidden
If .Value = 1 Then Sheets("SelectedList (Calcs Master)").Range("M:P").EntireColumn.Hidden = True
End With
End If
End Sub

I had trouble identifying where to add the "xlsheethidden" function for when the value = "2".

You'll notice I added another element to the code to hide a range when the value = 1 and changed the worksheet names. If I run the macro when "1" is entered, it hides columns M:P. However, if I type in "2" into F5, then columns M:P still remain hidden.

I think I need to add something like this:
If .Value = 2 Then Sheets("SelectedList (Calcs Master)").Range("M:P").EntireColumn.Hidden = False
Does that formula look correct? It works but I wanted to make sure I'm not overlooking an error that could occur.


Post the failing code that you tried.
 
Upvote 0
Try
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Application.Intersect(Target, Range("F5")) Is Nothing Then
        With Range("F5")
            Sheets("LPA Tables & Graphs (2 orgs)").Visible = IIf(.Value = "2", xlSheetVisible, xlSheetHidden)
            Sheets("LPA Tables & Graphs (1 org)").Visible = IIf(.Value = "1", xlSheetVisible, xlSheetHidden)
        End With
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,217,346
Messages
6,136,031
Members
449,979
Latest member
trinitybg10

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