How to combine two Worksheet_Change subs into one?

olivier5b

New Member
Joined
Nov 16, 2012
Messages
4
Hi everyone,

I need help to combine the two following subs that work fine separately into one that works. Your help would be appreciated. Thanks!

Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("B9")) Is Nothing Then Exit Sub
If Target.Count > 1 Then Exit Sub
Range("B19,B27").ClearContents
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("B11")) Is Nothing Then Exit Sub
If Target.Count > 1 Then Exit Sub
Range("E16,E27").ClearContents
End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Hi Olivier

Try:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Count > 1 Then Exit Sub

If Not Intersect(Target, Range("B9")) Is Nothing Then
    Range("B19,B27").ClearContents
End If

If Not Intersect(Target, Range("B11")) Is Nothing Then
    Range("E16,E27").ClearContents
End If

End Sub
 
Upvote 0
olivier5b,


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


Code:
Private Sub Worksheet_Change(ByVal Target As Range)
' hiker95, 12/26/2012
' http://www.mrexcel.com/forum/excel-questions/676322-how-combine-two-worksheet_change-subs-into-one.html#post3350133
If Intersect(Target, Range("B9,B11")) Is Nothing Then Exit Sub
If Target.Count > 1 Then Exit Sub
If Target.Row = 9 Then
  Range("B19,B27").ClearContents
ElseIf Target.Row = 11 Then
  Range("E16,E27").ClearContents
End If
End Sub
 
Upvote 0
Another way:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Select Case Target.Address
        Case "$B$9"
            Range("B19,B27").ClearContents
        Case "$B$11"
            Range("E16,E27").ClearContents
    End Select
End Sub
 
Upvote 0
Thanks shg, hiker95, pgc01 - I tried all 3 solutions and they all work. Very helpful, I really appreciate your time!
 
Upvote 0
olivier5b,

You are very welcome. Glad we could help.

Thanks for the feedback.

Come back anytime.
 
Upvote 0

Forum statistics

Threads
1,221,446
Messages
6,159,917
Members
451,603
Latest member
SWahl

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