Skip to main content

How to hide or unhide a specific worksheet based on cell value in another sheet?

Are there any methods for us to hide or unhide a specific worksheet tab based on a cell content in another sheet? For example, when I enter the text “Yes” in the cell G1 of Sheet2, I want the Sheet1 to be hidden, and when I enter “No”, the Sheet1 to be displayed at once. How could I solve this problem in Excel?

Hide or unhide a specific worksheet tab based on cell value with VBA code


arrow blue right bubble Hide or unhide a specific worksheet tab based on cell value with VBA code

To hide or unhide a specific worksheet tab based on a cell value in another worksheet, the following VBA code may do you a favor, please do as follows:

1. Go the worksheet which contains the cell value you want to hide another sheet based on.

2. Right click the sheet tab, and select View Code, in the popped out Microsoft Visual Basic for Applications window, please copy and paste the following code into the blank Module window, see screenshot:

VBA code: Hide or hide a worksheet tab based on cell value:

Private Sub Worksheet_Change(ByVal Target As Range)
If [G1] = "Yes" Then
Sheets("Sheet1").Visible = True
Else
Sheets("Sheet1").Visible = False
End If
End Sub

doc hide tab based on cell value 1

Note: In the above code, G1 and Yes are the cell and cell content that you want to base on, and Sheet1 is the specific sheet that you want to hide or unhide. You can change them to your need.

3. Then save and close this code, when you enter “No” or other text in cell G1, Sheet1 is hidden, but if you enter “Yes” in the cell, Sheet1 is displayed at once, see screenshots:

doc hide tab based on cell value 2
 1
doc hide tab based on cell value 3

 

Best Office Productivity Tools

🤖 Kutools AI Aide: Revolutionize data analysis based on: Intelligent Execution   |  Generate Code  |  Create Custom Formulas  |  Analyze Data and Generate Charts  |  Invoke Kutools Functions
Popular Features: Find, Highlight or Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |   Round without Formula ...
Super Lookup: Multiple Criteria VLookup    Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup ....
Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List ....
Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Ranges & Columns ...
Featured Features: Grid Focus   |  Design View   |   Big Formula Bar    Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells    Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ...
Top 15 Toolsets12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more

Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time.  Click Here to Get The Feature You Need The Most...

Description


Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!

 

Comments (24)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
I am trying to use this but it is coming up with "compile error - can't find project or library" and highlighting the cell where the Yes/No dropdown is situated. I think it may be due to this being a merged cell, is there any way around this?
This comment was minimized by the moderator on the site
I'm having an issue where I want the tab to show for a range of values, rather than just a YES or NO. When I try to repeat the IF statement in the sub, it gives me an error and when I try to list multiple values in the if statement, I get an error. Any ideas?

It almost seems like I need to use an IF/OR statement but not sure how that would work.

Private Sub Worksheet_Change(ByVal Target As Range)
If [K6] = "VS 1", "VS 2", "VS 3", VS 4" Then
Sheets("Page6").Visible = True
Else
Sheets("Page6").Visible = False
End If
End Sub
This comment was minimized by the moderator on the site
Hello, Margaret,
To solve your problem, please apply the below code, you should use Or to join the conditions.
Private Sub Worksheet_Change(ByVal Target As Range)
If [K6] = "VS 1" Or [K6] = "VS 2" Or [K6] = "VS 3" Or [K6] = "VS 4" Then
Sheets("Page6").Visible = True
Else
Sheets("Page6").Visible = False
End If
End Sub

Please have a try, hope it can help you!
This comment was minimized by the moderator on the site
Skyyang - this worked perfectly. Thank you for your prompt response!
This comment was minimized by the moderator on the site
I have a value in Cell B1 that if yes unhides sheet1 but also a value in B2 that's if yes unhides Sheet2 etc etc.. it hides/unhides the last sheet fine but not the rest, how would i go about having say 10 values unhiding 10 different sheets? thanks
This comment was minimized by the moderator on the site
Hello, Andy,
To solve your problem, please apply the below code:
Private Sub Worksheet_Change(ByVal Target As Range)
If [B2] = "Yes" Then
Sheets("Sheet1").Visible = True
Else
Sheets("Sheet1").Visible = False
End If
If [B3] = "Yes" Then
Sheets("Sheet2").Visible = True
Else
Sheets("Sheet2").Visible = False
End If
If [B4] = "Yes" Then
Sheets("Sheet3").Visible = True
Else
Sheets("Sheet3").Visible = False
End If
If [B5] = "Yes" Then
Sheets("Sheet4").Visible = True
Else
Sheets("Sheet4").Visible = False
End If
If [B6] = "Yes" Then
Sheets("Sheet5").Visible = True
Else
Sheets("Sheet5").Visible = False
End If
End Sub

Note: In the above code, you just need to copy the below scripts several times and change the cell reference and sheet name to your own.
If [B2] = "Yes" Then
Sheets("Sheet1").Visible = True
Else
Sheets("Sheet1").Visible = False
End If


Please have a try, hope it can help you!
This comment was minimized by the moderator on the site
This code works great, However I have 42 variables for 70 sheets so this code gets very long and stops working. Is there a way to make this code in sections as to not exceed the code size that VBA can handle? Or do I have to divide these into two separate excels?
This comment was minimized by the moderator on the site
Hello, Liz
Sorry, at present, there is not a good way for resolving your problem.
This comment was minimized by the moderator on the site
А я вот не могу понять, как такое сделать в гугл таблицах именно?
This comment was minimized by the moderator on the site
Hello friend,

You can open a new Google sheet, add a new sheet so that there is a "Sheet1" and a "Sheet2" then go to Tools > Script Editor. In the editor, paste the following.

function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName("Sheet1");
var sheet2 = ss.getSheetByName("Sheet2");

var cell1 = sheet1.getRange('B2');

if (cell1.getValue() == 2) {
sheet2.hideSheet();
}

if (cell1.getValue() == 3) {
sheet2.showSheet();
}
}

Please have a try.

Sincerely,
Mandy
This comment was minimized by the moderator on the site
I want to have a Menu/Table of Contents as my first worksheet and based on Yes/No answers - certain worksheets appear i.e. more than 1 becomes visible.
I'm struggling to have several worksheets appear based on the above code.
Can I use an AND function e.g. If X cell = "Yes" Then Make Y Sheet visible and Z Sheet and K Sheet?
Any advice greatly welcomed.
Regards
Helen
This comment was minimized by the moderator on the site
I am looking to hide or unhide sheets based on a cell value (Yes/No). The values are in a table (tblFileContents) and column D4:D25 and the sheet name is in A4:A25 on sheet <File Content>. The order of the items can be changed. Do you have a reference to an example that would allow me to do this?
This comment was minimized by the moderator on the site
I have the same issue and someone else provided a solution.
Make a table (format as table) with 2 columns, name the left column Sheets to show/hide, the right column is up  to you.Put all sheet names in left column
Then select View Code for this tab and enter Dim Changed As Range, c As Range, rMTS As Range
Dim i As Long
Dim bShowAll As Boolean

Set rMTS = Range("TblShowHide[Mark to Show]")
Set Changed = Intersect(Target, rMTS)
If Not Changed Is Nothing Then
bShowAll = Len(rMTS.Cells(1).Value) > 0
On Error Resume Next
For i = 2 To rMTS.Rows.Count
Sheets(Range("TblShowHide[Show/Hide Sheets]").Cells(i).Value).Visible = IIf(bShowAll, True, Len(rMTS.Cells(i).Value) > 0)
Next i
On Error GoTo 0
End If
End Sub


This comment was minimized by the moderator on the site
I will give this a try. Thank you very much!
This comment was minimized by the moderator on the site
Not sure what I need to change but it didn't work for me.
This comment was minimized by the moderator on the site
Hi Jean,
yes I have an example I attached here. Rename the file to example.xlsm (it is not zipped, but had to rename to upload)
On the menu tab there is a table with the various tab names, make an x or any other character to show the tab, if you remove the character, the tab hides.
Hope that helps
This comment was minimized by the moderator on the site
This contains several items but no excel file.
This comment was minimized by the moderator on the site
HiI have a workbook with multiple named tabs linked to an index sheet at the front. The user can select the sheets they want to use by checking a box next to the sheet name - blank, N/A or Yes (dropdown list). Is there a way of adapting this code so that the sheet is visible if the check box is blank or contains "Yes" but not visible if the checkbox contains "N/A".
I have tried but do not know enough about VBA to make it work. Thanks
This comment was minimized by the moderator on the site
I did 2 worksheets as follows:
Private Sub Worksheet_Change(ByVal Target As Range)
If [C20] = "Yes" Then
Sheets("sheet1").Visible = True
Else
Sheets("sheet1").Visible = False
End If
If [C22] = "Yes" Then
Sheets("sheet2").Visible = True
Else
Sheets("sheet2").Visible = False
End If
End Sub
This comment was minimized by the moderator on the site
Hi Team,

I need similar code I have table range B10 : G40 where in in Column B11 I have sheets name and Column C11:G40 I have data validations as Yes /No. I need code to hide or visible sheets as per columns B if Its YES in Column C:G. Please help.

Sheets CIPS PE RE BANKS IM
IFRS 1 Yes Yes Yes Yes Yes
IFRS 2 Yes Yes Yes Yes Yes
IFRS 3 Yes Yes Yes Yes Yes
IFRS 5 Yes Yes Yes Yes Yes
IFRS 6 No No No No No
IFRS 7 Yes Yes No Yes Yes
IFRS 13 No Yes No Yes Yes
IFRS 14 No No No No No
IFRS 15 Yes No No No No
IFRS 16 Yes No Yes No No
IAS 1 Yes Yes Yes Yes Yes
IAS 2 No No Yes No No
IAS 7 Yes Yes Yes Yes Yes
IAS 8 Yes Yes Yes Yes Yes
IAS 10 Yes Yes Yes Yes Yes
IAS 12 No No No No No
IAS 16 No No No No No
IAS 19 No No No No No
IAS 20 Yes Yes Yes Yes Yes
IAS 21 Yes Yes Yes Yes Yes
IAS 23 Yes Yes Yes Yes Yes
IAS 24 Yes Yes Yes Yes Yes
IAS 27 Yes Yes Yes Yes Yes
IAS 29 Yes Yes Yes Yes Yes
IAS 32 No No No Yes Yes
IAS 34 Yes Yes Yes Yes Yes
IAS 36 Yes Yes Yes Yes No
IAS 38 Yes No No Yes No
IAS 40 Yes No Yes Yes No
IAS 41 No No No No No
This comment was minimized by the moderator on the site
I want to see if you can help with my issue. I've had essentially this exact code in a sheet I use but with an or function so the answer can be "yes" or "true". However, the sheet that is being hidden/unhidden sometimes re-hides itself for unknown reasons. It will unhide when I type yes, but when I go to use it later on it is hidden again, even though the cell value is still yes. Any idea why this might be happening, and/or how to fix it?
Code for reference:
Private Sub Worksheet_Change(ByVal Target As Range)'Hides/unhides Calibration page
If [B18] = "Yes" Or Target.Value = "True" Then
Sheets("XXX Verification").Visible = True
Else
Sheets("XXX Verification").Visible = False
End If

This comment was minimized by the moderator on the site
Very helpful!
This comment was minimized by the moderator on the site
Hello
Thank you for the tip. I need to do the same scenario but not on one cell only (G1 in this case) but on all cells of column G. I have tried with "Range" but it didn't work.

Private Sub Worksheet_Change(ByVal Target As Range)
If Range("X2:X100") = "" Then
Sheets("EU TASK BASED MEASUREMENTS").Visible = False
Else
Sheets("EU TASK BASED MEASUREMENTS").Visible = True
End If
End Sub


Thank you in advance
This comment was minimized by the moderator on the site
Any chance this was answered? I am also running into this scenario where a whole range comes into play instead of just one cell... I used this same coding with the same results.
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations