Multiple WorksheetChange Events

whothemannow

New Member
Joined
Feb 23, 2021
Messages
3
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!
 

Some videos you may like

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,301
Office Version
  1. 365
Platform
  1. Windows
Hi & welcome to MrExcel.
Should the C8 in the 1st code really be C10?
 

whothemannow

New Member
Joined
Feb 23, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,301
Office Version
  1. 365
Platform
  1. Windows
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
 
Solution

whothemannow

New Member
Joined
Feb 23, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
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 :)
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,301
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,107
Messages
5,622,782
Members
415,927
Latest member
vedasinternational

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
Top