Combining multiple Worksheet Change Events

Milos

Board Regular
Joined
Aug 28, 2016
Messages
121
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have two worksheet changes that I am having trouble to combine so that the changes work at the same time. The two changes do affect the same cells, but one hides the whole row and one stops specific cells from being deleted.

Here are the two codes:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Intersect(Target, Range("F28:J36,F45:J52,F58:J59")) Is Nothing Then Exit Sub
    On Error GoTo ExitPoint
    Application.EnableEvents = False
    If Not IsDate(Target(1)) Then
        Application.Undo
        MsgBox " You can't delete contents from this cell." _
        , vbCritical, "Message alert!"
    End If
  
ExitPoint:
    Application.EnableEvents = True
End Sub

Code:
  Rows("28:36").Hidden = LCase(Range("B5").Value) = 1
  Rows("45:52").Hidden = LCase(Range("B5").Value) = 1
  Rows("58:59").Hidden = LCase(Range("B6").Value) = "no"

Can somebody please help me combine the two codes to work in cohesion?

Thanks,
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi,

I am not sure I am fully understanding the problem but does this help?

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo ExitPoint
    Application.EnableEvents = False
    If Not Intersect(Target, Range("F28:J36,F45:J52,F58:J59")) Is Nothing Then
        If Not IsDate(Target(1)) Then
            Application.Undo
            MsgBox " You can't delete contents from this cell." _
            , vbCritical, "Message alert!"
        End If
    End If
    Rows("28:36").Hidden = LCase(Range("B5").Value) = 1
    Rows("45:52").Hidden = LCase(Range("B5").Value) = 1
    Rows("58:59").Hidden = LCase(Range("B6").Value) = "no"
ExitPoint:
    Application.EnableEvents = True
End Sub

It runs the first part of the code whenever the two "IF" statements are satisfied (Note, I added a "not" in the first).
Then it runs the second code every time the worksheet changes.

Note: I have not tested the code.


Regards,
 
Upvote 0

Forum statistics

Threads
1,224,815
Messages
6,181,135
Members
453,021
Latest member
Justyna P

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