Creating Mutliple Worksheet_Change Event Procedures

Sundae

New Member
Joined
Jul 25, 2011
Messages
44
Hi there, I am finding so difficult to start a new thread. My question is similar to the above. I have the following code:

Private Sub Worksheet_Change(ByVal Target As Range)
If Range("a15").Value = "Exempt" And Range("F6").Value = "Exempt" Then
Rows("19").EntireRow.Hidden = True
Rows("21").EntireRow.Hidden = True
ElseIf Range("a15").Value = "Exempt" And Range("F6").Value = "Non-exempt" Then
Rows("19").EntireRow.Hidden = False
Rows("21").EntireRow.Hidden = False
ElseIf Range("a15").Value = "Non-exempt" And Range("F6").Value = "Exempt" Then
Rows("19").EntireRow.Hidden = False
Rows("21").EntireRow.Hidden = False
End If
End Sub

I need to create a second change action, but I get ambiguous name detected vba error, where do I change my code name? Thank you!


Split from: https://www.mrexcel.com/forum/excel-questions/1068094-hiding-rows-vba-question.html
 
Last edited by a moderator:
OK, I think I may see what you are getting at. Let's look at a possible example.

Let's say that A15 is "Exempt" and F6 is "Non-exempt".
And here is your code, formatted nicely.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Range("a15").Value = "Exempt" And Range("F6").Value = "Exempt" Then
        rows("19").EntireRow.Hidden = True
        rows("21").EntireRow.Hidden = True
    ElseIf Range("a15").Value = "Exempt" And Range("F6").Value = "Non-exempt" Then
[COLOR=#ff0000]        rows("19").EntireRow.Hidden = False
        rows("21").EntireRow.Hidden = False[/COLOR]
    ElseIf Range("a15").Value = "Non-exempt" And Range("F6").Value = "Exempt" Then
        rows("19").EntireRow.Hidden = False
        rows("21").EntireRow.Hidden = False
    ElseIf Range("a15").Value = "Exempt" Then
[COLOR=#ff0000]        rows("36:37").EntireRow.Hidden = True[/COLOR]
    ElseIf Range("a15").Value = "Non-exempt" Then
        rows("41:42").EntireRow.Hidden = True
    End If

End Sub
Are you saying that you want BOTH blocks in red to run?

If so, then you want to remove the last two things you added from the big, nested IF statement you added, and make them their own block.
Because in a large, nested IF ELSEIF statement, once it meets one condition, it stops there and does not evaluate the rest of your block.

So I think this may be what you are looking for:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

'   Block 1
    If Range("a15").Value = "Exempt" And Range("F6").Value = "Exempt" Then
        rows("19").EntireRow.Hidden = True
        rows("21").EntireRow.Hidden = True
    ElseIf Range("a15").Value = "Exempt" And Range("F6").Value = "Non-exempt" Then
        rows("19").EntireRow.Hidden = False
        rows("21").EntireRow.Hidden = False
    ElseIf Range("a15").Value = "Non-exempt" And Range("F6").Value = "Exempt" Then
        rows("19").EntireRow.Hidden = False
        rows("21").EntireRow.Hidden = False
    End If
    
'   Block 2
    If Range("a15").Value = "Exempt" Then
        rows("36:37").EntireRow.Hidden = True
    ElseIf Range("a15").Value = "Non-exempt" Then
        rows("41:42").EntireRow.Hidden = True
    End If

End Sub
 
Last edited:
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
You are AWESOME!!!! Thank you so much! I am so grateful for this forum and the assistance from experts like you provide.

Happy New Year!!
 
Upvote 0
You are welcome.

Glad I was able to help.
:)
 
Upvote 0

Forum statistics

Threads
1,223,920
Messages
6,175,378
Members
452,638
Latest member
Oluwabukunmi

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