How to link two criteria to hide/unhide sheets?

povictory

New Member
Joined
May 28, 2015
Messages
45
Hello,

Very much a VBA novice here. I have some code below that has been very helpful in hiding/unhiding sheets based on input in a cell. However, I'm looking for a way to combine the two criteria so that the sheet will be unhidden if *both* M26=YES and if M9<>0. If either of those conditions are *NOT* met, I'd like for the sheet to be hidden. I've been doing a lot searching around and experimenting but I haven't been able to get anywhere. Any advice on how I can modify the code below to make the visibility of the sheet dependent on both conditions?

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo Whoa
   
    Application.EnableEvents = False
   
    If Not Intersect(Target, Range("M26")) Is Nothing Then _
    Sheet15.Visible = (UCase(Trim(Range("M26").Value2)) = "YES")
    
    If Not Intersect(Target, Range("M9")) Is Nothing Then _
    Sheet15.Visible = (Len(Trim(Range("M9").Value2)) <> 0)  
    
   
Letscontinue:
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume Letscontinue
End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
This could be a solution:
VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo Whoa
    Application.EnableEvents = False
    If Not Intersect(Target, Range("M26", "M9")) Is Nothing Then
        If (UCase(Trim(Range("M26").Value2)) = "YES") And (Len(Trim(Range("M9").Value2)) <> 0) Then
            Sheet15.Visible = True
        Else
            Sheet15.Visible = xlSheetHidden
        End If
    End If
Letscontinue:
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume Letscontinue
End Sub
 
Upvote 0
Solution
Thanks for the positive feedback(y), glad having been of some help.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,248
Members
452,623
Latest member
cliftonhandyman

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