Multiple worksheet change events in one sheet

nparsons75

Well-known Member
Joined
Sep 23, 2013
Messages
1,254
Office Version
  1. 2016
Hi,

I have a worksheet change event that works perfectly as is. Basically it looks for a change within a range and then name and time stamps in to seperate cells. What I need is to have this work numerous times, so I have multiple ranges, and multiple name and time stamp cells per each range. My current code is:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)ActiveSheet.Unprotect Password:="******"
Application.EnableEvents = False
If Intersect(Target, Range("A14:z35")) Is Nothing Then Exit Sub
Range("e35").Value = Environ("UserName")
Range("k35").Value = Now
ActiveSheet.Protect Password:="******"
Letscontinue:
Application.EnableEvents = True
Exit Sub
End Sub

I thought I would try this but the second change overides the first.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)ActiveSheet.Unprotect Password:="oeestat"
Application.EnableEvents = False
If Intersect(Target, Range("A14:z35")) Is Nothing Then Exit Sub
Range("e35").Value = Environ("UserName")
Range("k35").Value = Now
If Intersect(Target, Range("B42:Z55")) Is Nothing Then Exit Sub
Range("e10").Value = Environ("UserName")
Range("k10").Value = Now
ActiveSheet.Protect Password:="oeestat"
Letscontinue:
Application.EnableEvents = True
Exit Sub
End Sub

Thanks in advance for any help.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
You need to get rid of your "EXIT SUB" commands, as if that condition is not met, it will exit the sub and never get to look at anything below it (your other conditions).

So you need to change your approach. Instead of structuring each one like this:
Code:
If Intersect(Target, Range("A14:z35")) Is Nothing Then Exit Sub
It needs to be a block like this:
Code:
If Not Intersect(Target, Range("A14:z35")) Is Nothing Then
   'steps to perform if target cell found in A14:Z35
End If
So, if you have multiple conditions, you would have multiple blocks like this.
 
Upvote 0
Solution
You are welcome!
 
Upvote 0
I would like to use multiple Worksheet Change code in same worksheet. How to do that? My code is as below:

Private Sub Worksheet_Change(ByVal Target As Range)
'Pivot table filter based on cell value
Dim xPTable As PivotTable
Dim xPFile As PivotField
Dim xStr As String
On Error Resume Next
If Intersect(Target, Range("D20:D21")) Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Set xPTable = Worksheets("Sheet1").PivotTables("PivotTable2")
Set xPFile = xPTable.PivotFields("Designation")
xStr = Target.Text
xPFile.ClearAllFilters
xPFile.CurrentPage = xStr
Application.ScreenUpdating = True
End Sub

Private Sub Worksheet_Change2(ByVal Target As Range)
'Pivot table filter based on cell value 2
Dim xPTable As PivotTable
Dim xPFile As PivotField
Dim xStr As String
On Error Resume Next
If Intersect(Target, Range("H20:H21")) Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Set xPTable = Worksheets("Sheet1").PivotTables("PivotTable2")
Set xPFile = xPTable.PivotFields("Offering")
xStr = Target.Text
xPFile.ClearAllFilters
xPFile.CurrentPage = xStr
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Just like in my previous reply, you can only have one Worksheet_Change event procedure code per worksheet, so you need to combine them into one.
And you need to change the block of the first one so that it does not exit if your first condition is not met.

You can do that by changing this line:
VBA Code:
If Intersect(Target, Range("D20:D21")) Is Nothing Then Exit Sub
to this:
VBA Code:
If Not Intersect(Target, Range("D20:D21")) Is Nothing Then
and making it an IF...END IF block.

Also, since you are using the same variable names in both blocks, you only want one round of variable declarations.

So your final code will look something like this.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim xPTable As PivotTable
Dim xPFile As PivotField
Dim xStr As String
On Error Resume Next

'***FIRST CHECK***
'Pivot table filter based on cell value
If Not Intersect(Target, Range("D20:D21")) Is Nothing Then
    Application.ScreenUpdating = False
    Set xPTable = Worksheets("Sheet1").PivotTables("PivotTable2")
    Set xPFile = xPTable.PivotFields("Designation")
    xStr = Target.Text
    xPFile.ClearAllFilters
    xPFile.CurrentPage = xStr
    Application.ScreenUpdating = True
End If

'***SECOND CHECK***
'Pivot table filter based on cell value 2
If Not Intersect(Target, Range("H20:H21")) Is Nothing Then
    Application.ScreenUpdating = False
    Set xPTable = Worksheets("Sheet1").PivotTables("PivotTable2")
    Set xPFile = xPTable.PivotFields("Offering")
    xStr = Target.Text
    xPFile.ClearAllFilters
    xPFile.CurrentPage = xStr
    Application.ScreenUpdating = True
End If

On Error GoTo 0

End Sub
 
Upvote 0
Just like in my previous reply, you can only have one Worksheet_Change event procedure code per worksheet, so you need to combine them into one.
And you need to change the block of the first one so that it does not exit if your first condition is not met.

You can do that by changing this line:
VBA Code:
If Intersect(Target, Range("D20:D21")) Is Nothing Then Exit Sub
to this:
VBA Code:
If Not Intersect(Target, Range("D20:D21")) Is Nothing Then
and making it an IF...END IF block.

Also, since you are using the same variable names in both blocks, you only want one round of variable declarations.

So your final code will look something like this.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim xPTable As PivotTable
Dim xPFile As PivotField
Dim xStr As String
On Error Resume Next

'***FIRST CHECK***
'Pivot table filter based on cell value
If Not Intersect(Target, Range("D20:D21")) Is Nothing Then
    Application.ScreenUpdating = False
    Set xPTable = Worksheets("Sheet1").PivotTables("PivotTable2")
    Set xPFile = xPTable.PivotFields("Designation")
    xStr = Target.Text
    xPFile.ClearAllFilters
    xPFile.CurrentPage = xStr
    Application.ScreenUpdating = True
End If

'***SECOND CHECK***
'Pivot table filter based on cell value 2
If Not Intersect(Target, Range("H20:H21")) Is Nothing Then
    Application.ScreenUpdating = False
    Set xPTable = Worksheets("Sheet1").PivotTables("PivotTable2")
    Set xPFile = xPTable.PivotFields("Offering")
    xStr = Target.Text
    xPFile.ClearAllFilters
    xPFile.CurrentPage = xStr
    Application.ScreenUpdating = True
End If

On Error GoTo 0

End Sub
Awesome!! Thanks a lot.
 
Upvote 0
You are welcome.
 
Upvote 0

Forum statistics

Threads
1,215,692
Messages
6,126,230
Members
449,303
Latest member
grantrob

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