ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Multiple change events on same sheet (https://www.excelbanter.com/excel-programming/346835-multiple-change-events-same-sheet.html)

JG Scott

Multiple change events on same sheet
 
I'm sure this is an easy question. I already have a
Worksheet_Change(ByVal Target as Range) procedure for my "Sales"
worksheet. I need to add code to respond to some additional changes on
the sheet. How is that done? (I added the code in the same procedure,
but it doesn't work.)

Thanks.


Bob Phillips[_6_]

Multiple change events on same sheet
 
Use Else, like this

Private Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo ws_exit:
Application.EnableEvents = False
If Not Intersect(Target, Me.Range("H1:H10")) Is Nothing Then
With Target
'do your stuff
End With
ElseIf Not Intersect(Target, Me.Range("M5:M15")) Is Nothing Then
With Target
'do your stuff
End With
End If

ws_exit:
Application.EnableEvents = True
End Sub

'This is worksheet event code, which means that it needs to be
'placed in the appropriate worksheet code module, not a standard
'code module. To do this, right-click on the sheet tab, select
'the View Code option from the menu, and paste the code in.

--

HTH

RP
(remove nothere from the email address if mailing direct)


"JG Scott" wrote in message
ups.com...
I'm sure this is an easy question. I already have a
Worksheet_Change(ByVal Target as Range) procedure for my "Sales"
worksheet. I need to add code to respond to some additional changes on
the sheet. How is that done? (I added the code in the same procedure,
but it doesn't work.)

Thanks.




Tom Ogilvy

Multiple change events on same sheet
 
Add the code in the same procedure in a case statement as one way.

Private Sub Worksheet_Change(ByVal Target As Range)
Select Case Target.Address(0,0)
Case "A1"
' action to take if A1 is change
Case "B9","C5"
' action to take if B9 or C5 is changed
Case "F2"
' action to take if F2 is changed
End Select
End Sub

--
Regards,
Tom Ogilvy

"JG Scott" wrote in message
ups.com...
I'm sure this is an easy question. I already have a
Worksheet_Change(ByVal Target as Range) procedure for my "Sales"
worksheet. I need to add code to respond to some additional changes on
the sheet. How is that done? (I added the code in the same procedure,
but it doesn't work.)

Thanks.




JG Scott

Multiple change events on same sheet
 
Thanks. Very helpful.


David

Multiple change events on same sheet
 
Thanks Bob...I was having the same issue with two change events and your
ELSEIF on the 2nd event got me what I needed!
Thanks much!

"Bob Phillips" wrote:

Use Else, like this

Private Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo ws_exit:
Application.EnableEvents = False
If Not Intersect(Target, Me.Range("H1:H10")) Is Nothing Then
With Target
'do your stuff
End With
ElseIf Not Intersect(Target, Me.Range("M5:M15")) Is Nothing Then
With Target
'do your stuff
End With
End If

ws_exit:
Application.EnableEvents = True
End Sub

'This is worksheet event code, which means that it needs to be
'placed in the appropriate worksheet code module, not a standard
'code module. To do this, right-click on the sheet tab, select
'the View Code option from the menu, and paste the code in.

--

HTH

RP
(remove nothere from the email address if mailing direct)


"JG Scott" wrote in message
ups.com...
I'm sure this is an easy question. I already have a
Worksheet_Change(ByVal Target as Range) procedure for my "Sales"
worksheet. I need to add code to respond to some additional changes on
the sheet. How is that done? (I added the code in the same procedure,
but it doesn't work.)

Thanks.






All times are GMT +1. The time now is 01:14 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com