Call sheet based on YES/NO

Adele05

New Member
Joined
Mar 17, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I have a workbook with many sheets. I have an index worksheet that list the names of the worksheets in column B and a Yes/No question in column C. I am trying a VBA a code that when I type "Yes" it will open the worksheet that it corresponds to. When I type "No" it will hide the worksheet. The names in the list exactly match the name of the worksheets.

Range is from B1to C36
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Use WorksheetChange Event:
1) Rightclick on the tab with the sheetname Index; selech Display code. This will open the macro editor at "the right position"
2) Copy the following code and paste it into the empty frame of the macro editor

Then return to Index and try
This should show the sheet when Yes is typed, and hide it in all the other conditions
 
Upvote 0
You was certainly asking "where is my code?"
Ehmm... It was left in my test workbook, and I just noticed it now by accident
Here it is:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim myC As Range, ckArea As String
'
ckArea = "C2:C100"         '<<< The range with  the sheet names
'
If Not Application.Intersect(Target, Range(ckArea)) Is Nothing Then
    For Each myC In Application.Intersect(Target, Range(ckArea))
        If myC.Value = "Yes" Then
            Sheets(myC.Offset(0, -1).Value).Visible = True
        Else
            Sheets(myC.Offset(0, -1).Value).Visible = False
        End If
    Next myC
End If
End Sub
 
Upvote 0
Solution
@Anthony47 Would this work with checkboxes instead of yes and no? Meaning when the checkbox is checked that sheet/tab would show and if left unchecked it would be hidden?

If so how would that change the code? What would the VBA input look like?
 

Attachments

  • Screen Shot 2023-07-31 at 7.42.57 PM.png
    Screen Shot 2023-07-31 at 7.42.57 PM.png
    144.1 KB · Views: 2
Upvote 0
It can be done, but...
A checkbox doesn't trigger a WorksheetChange event, so you need to use for example the CheckBox_Click event itself, for each of the checkboxes.
This means:
-you create the CheckBoxes, using the ActiveX control
-open the worksheet vba module (rightclick the tab with the name of the worksheet, select Display code)
-with reference to the attached image, click the highlighted drop down; select each of the checkboxes, and a Private Sub CheckBoxX_Click will be initialized
-add in this Sub the instruction to show/hide the associated worksheet; for example
VBA Code:
Private Sub CheckBox1_Click()
    Sheets("Foglio2").Visible = CheckBox1.Value
End Sub

Private Sub CheckBox2_Click()
    Sheets("Foglio5").Visible = CheckBox1.Value
End Sub

'etc etc

Other tricks could be developed, but I think the advantages would be marginal
 

Attachments

  • MrEX_Immagine 2023-08-01 094341.jpg
    MrEX_Immagine 2023-08-01 094341.jpg
    35.9 KB · Views: 2
Upvote 0

Forum statistics

Threads
1,214,960
Messages
6,122,479
Members
449,088
Latest member
Melvetica

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