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
 
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"
This means 2 things

1. The code is working as expected
2. You are passing the wrong sheet and hence you are getting the "Subscript out of range". This error means that "Sheet9" doesn't exist. Please recheck.
 
Upvote 0

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
I asked the same question in my earlier post and never got an answer:

The script works for me. Do you have a sheet named "Sheet9")
 
Upvote 0
These two sheets are not the same:
One is named "Sheet9"
The other one is sheet9
VBA Code:
Sub My_Sheet()
'Modified 3/20/2022  2:36:26 AM  EDT
Sheet9.Range("A1").Value = "Alpha"
Sheets("Sheet9").Range("A1").Value = "Alpha"
End Sub
 
Upvote 0
These two sheets are not the same:
One is named "Sheet9"
The other one is sheet9
VBA Code:
Sub My_Sheet()
'Modified 3/20/2022  2:36:26 AM  EDT
Sheet9.Range("A1").Value = "Alpha"
Sheets("Sheet9").Range("A1").Value = "Alpha"
End Sub
Yes, I see that now. I'm trying to refer to the object sheet9. There is not a sheet named "Sheet9". Apologies for the confusion.
 
Upvote 0
This means 2 things

1. The code is working as expected
2. You are passing the wrong sheet and hence you are getting the "Subscript out of range". This error means that "Sheet9" doesn't exist. Please recheck.
I'm trying to refer to the object sheet9. You are correct in that there is not a tab named "Sheet9". Sorry about the confusion.
 
Upvote 0
I'm trying to refer to the object sheet9. You are correct in that there is not a tab named "Sheet9". Sorry about the confusion.

Can you do this for me? Open VBA Editor and then post a screenshot of the Project Explorer on the left side?
 
Upvote 0

Attachments

  • Capture.PNG
    Capture.PNG
    9.2 KB · Views: 5
Upvote 0
In my code in Post #3

Change

VBA Code:
Worksheets("Sheet9").Visible

to

VBA Code:
Sheet9.Visible

or to

VBA Code:
Worksheets("h").Visible
 
Upvote 0
In my code in Post #3

Change

VBA Code:
Worksheets("Sheet9").Visible

to

VBA Code:
Sheet9.Visible

or to

VBA Code:
Worksheets("h").Visible
Thank you - this works great when selecting Yes or No. However, when I delete data out of the cell I get a "Type mismatch" popup and the tab doesn't disappear. Is there a tweak I can make to do that?
 
Upvote 0
Thank you - this works great when selecting Yes or No. However, when I delete data out of the cell I get a "Type mismatch" popup and the tab doesn't disappear. Is there a tweak I can make to do that?
What do you mean by "when I delete data out of the cell". If you mean pressing the "DELETE" button when the cell is active, then the code will work as expected.
 
Upvote 0

Forum statistics

Threads
1,215,767
Messages
6,126,773
Members
449,336
Latest member
p17tootie

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