Multiple WorksheetChange Events

whothemannow

New Member
Joined
Feb 23, 2021
Messages
8
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi everyone,

I've tried a number of solutions online and I can't seem to find one that actually works for me. I am trying to combine two WorksheetChange events on the same sheet in VBA. I am a newcomer to VBA, so I might be missing something, or just a more efficient way that I could be doing this.
Essentially, I want to have 2 events which largely are the same concept, depending on the input of a certain cell, this hides or shows different rows.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Intersect(Target, Range("C10")) Is Nothing Or Target.Cells.Count > 1 Then
        Exit Sub

    ElseIf Range("C10").Value = "Select One" Then
        Rows("11:17").EntireRow.Hidden = True

    ElseIf Range("C8").Value = "Yes" Then
        Rows("11:16").EntireRow.Hidden = False
        Rows("17").EntireRow.Hidden = True

    ElseIf Range("C8").Value = "No" Then
        Rows("11:16").EntireRow.Hidden = True
        Rows("17").EntireRow.Hidden = False

    End If

End Sub

Here, I want to show Row 17 when the value in C10 is Yes, 11-16 when it is No, and nothing when it is Select One.
I want to combine this with:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Intersect(Target, Range("C19")) Is Nothing Or Target.Cells.Count > 1 Then
        Exit Sub

    ElseIf Range("C19").Value = "Select One" Then
        Rows("20:22").EntireRow.Hidden = True

    ElseIf Range("C19").Value = "Yes" Then
        Rows("21").EntireRow.Hidden = False
        Rows("22").EntireRow.Hidden = True

    ElseIf Range("C19").Value = "No" Then
        Rows("20:21").EntireRow.Hidden = True
        Rows("22").EntireRow.Hidden = False

    End If

End Sub
Here, I want Row 22 to be shown when C19 is Yes, 20-21 when it is No, and nothing when it is Select One.
Thanks in advance!
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi & welcome to MrExcel.
Should the C8 in the 1st code really be C10?
 
Upvote 0
Hi & welcome to MrExcel.
Should the C8 in the 1st code really be C10?
Hi thanks, yes it should, I just made a mistake when copying over from an older copy. In the Excel file it is C10. It's mainly the second code that I cannot get to work.
 
Upvote 0
Ok, how about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Address(0, 0) = "C10" Then
      Select Case Target
         Case "Select One"
            Rows("11:17").EntireRow.Hidden = True
         Case "Yes"
            Rows("11:16").EntireRow.Hidden = False
            Rows("17").EntireRow.Hidden = True
         Case "No"
            Rows("11:16").EntireRow.Hidden = True
            Rows("17").EntireRow.Hidden = False
      End Select
   ElseIf Target.Address(0, 0) = "C19" Then
      Select Case Target
         Case "Select One"
            Rows("20:22").EntireRow.Hidden = True
         Case "Yes"
            Rows("21").EntireRow.Hidden = False
            Rows("22").EntireRow.Hidden = True
         Case "No"
            Rows("20:21").EntireRow.Hidden = True
            Rows("22").EntireRow.Hidden = False
      End Select
   End If
End Sub
 
Upvote 0
Solution
Ok, how about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Address(0, 0) = "C10" Then
      Select Case Target
         Case "Select One"
            Rows("11:17").EntireRow.Hidden = True
         Case "Yes"
            Rows("11:16").EntireRow.Hidden = False
            Rows("17").EntireRow.Hidden = True
         Case "No"
            Rows("11:16").EntireRow.Hidden = True
            Rows("17").EntireRow.Hidden = False
      End Select
   ElseIf Target.Address(0, 0) = "C19" Then
      Select Case Target
         Case "Select One"
            Rows("20:22").EntireRow.Hidden = True
         Case "Yes"
            Rows("21").EntireRow.Hidden = False
            Rows("22").EntireRow.Hidden = True
         Case "No"
            Rows("20:21").EntireRow.Hidden = True
            Rows("22").EntireRow.Hidden = False
      End Select
   End If
End Sub
It works! Thanks a bunch :)
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,213,554
Messages
6,114,280
Members
448,562
Latest member
Flashbond

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