VBA code to make tab visible depending on input in another sheet

povictory

New Member
Joined
May 28, 2015
Messages
41
Hello - I've done a lot of searching on various forums but I'm unable to figure out why my code isn't working. I am trying to have a tab show up as visible in the workbook if "Yes" is selected in another cell. If "No" or nothing is selected in the dropdown, I want the tab to remain hidden. I keep getting run-time error 9 and I'm not sure what I need to change to get it to work. Any assistance is greatly appreciated. Thanks!

VBA Code:
Sub Tab_Prompts()

If Sheet1.Range("H16") = "Yes" Then
    Worksheets("Sheet9").Visible = True
  Else
    Worksheets("Sheet9").Visible = False
        
  End If

End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
@povictory If your specified sheets exist then that code should be ok.

However, why the two different methods used to refer to the sheets?
Does Sheet1 actually exist or might you have deleted it?
NB Sheet1 is not the same as Sheets(1) which would refer to the first sheet in your array of tabs.
Do you have a typo sheet name for "Sheet9" ?
 
Upvote 0
You can use the Worksheet_Change event. Paste this code in the Sheet Code module of Sheet1.

Excel Formula:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo Whoa
   
    Application.EnableEvents = False
   
    If Not Intersect(Target, Range("H16")) Is Nothing Then _
    Worksheets("Sheet9").Visible = (Target = UCase("YES"))
   
Letscontinue:
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume Letscontinue
End Sub
 
Upvote 0
Solution
@povictory If your specified sheets exist then that code should be ok.

However, why the two different methods used to refer to the sheets?
Does Sheet1 actually exist or might you have deleted it?
NB Sheet1 is not the same as Sheets(1) which would refer to the first sheet in your array of tabs.
Do you have a typo sheet name for "Sheet9" ?
Ah...this is helpful. I sort of cobbled the code together by looking at issues that were similar but not quite the same and I didn't realize that I had two different methods in there...dumb move on my part!

So I fixed that and the code runs successfully now. However, I have to manually run it in order to make it work. It doesn't update upon inputting a value or deleting a value in cell H16. Any thoughts on how to have it run when there is input in that cell or the input is deleted?


VBA Code:
Sub Tab_Prompts()

If Sheet1.Range("H16") = "Yes" Then
    Sheet9.Visible = True
  Else
    Sheet9.Visible = False
        
  End If

End Sub
 
Upvote 0
However, I have to manually run it in order to make it work. It doesn't update upon inputting a value or deleting a value in cell H16. Any thoughts on how to have it run when there is input in that cell or the input is deleted?
Seems like you missed my post?
 
Upvote 0
On the basis that you will be in Sheet1 when changing Value in H16 then the below code in the Sheet1 code module.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Exit if not single cell
If Target.CountLarge > 1 Then Exit Sub
'Proceed if cell is H16
If Not Intersect(Target, Range("H16")) Is Nothing Then Sheet9.Visible = (Target = "Yes")
End Sub
 
Upvote 0
You can use the Worksheet_Change event. Paste this code in the Sheet Code module of Sheet1.

Excel Formula:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo Whoa
 
    Application.EnableEvents = False
 
    If Not Intersect(Target, Range("H16")) Is Nothing Then _
    Worksheets("Sheet9").Visible = (Target = UCase("YES"))
 
Letscontinue:
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume Letscontinue
End Sub

A small typo in my post...

VBA Code:
Target = UCase("YES")

Should be

VBA Code:
 UCase(Trim(Target.Value2)) = "YES"
 
Upvote 0
Seems like you missed my post?
Sorry! I did miss your post. Unfortunately it doesn't seem to be working for me (even after correcting for the typo). Selecting "Yes" or "No" or clearing the cell doesn't hide/unhide the sheet9. I must still be doing something wrong.

When I enter Yes or No in the cell I get an Excel pop-up that says "Subscript out of range"
 
Last edited:
Upvote 0
On the basis that you will be in Sheet1 when changing Value in H16 then the below code in the Sheet1 code module.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Exit if not single cell
If Target.CountLarge > 1 Then Exit Sub
'Proceed if cell is H16
If Not Intersect(Target, Range("H16")) Is Nothing Then Sheet9.Visible = (Target = "Yes")
End Sub
This is pretty close to what I'm trying to do! Sheet9 appears when selecting "Yes" and disappears when selecting "No". Only other functionality I'm looking for is to have sheet9 be invisible if H16 is empty. Also, if Yes is selected and sheet9 is visible, I'd like it to disappear when Yes is deleted out of H16. Any suggestions to add that?
 
Upvote 0

Forum statistics

Threads
1,214,896
Messages
6,122,132
Members
449,066
Latest member
Andyg666

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