Need clarification on Code "Private Sub Worksheet_Change(ByVal Target As Range)".

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
how many Private Sub Worksheet_Change(ByVal Target As Range) code can we have in a sheet? Can i have 2? Is that possible?

Suppose I want the target to be B1 and then other range is D2:D7...

D2:D7...has diffferent code and B1 has different code again so confused..:confused:

Thanks for helping!
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Use If Statements to form your conditions

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'FOR A SINGLE CELL
If Target.Address = "$B$1" Then
    MsgBox "B1 Code Here"
End If

'FOR A RANGE OF CELLS
If Not Intersect(Target, Range("D2:D7")) Is Nothing Then
    MsgBox "D2:D7 Code Here"
End if

End Sub
 
Last edited:
Upvote 0
Hi Pedie,
The direct answer to your question is 'Nope. We can only have one Worksheet_Change routine per worksheet.'

The good news is that Nogslaw has shown one of the ways to incorporate both conditions into a single routine, so... well, there it is.

Have a good one. ;)
 
Upvote 0
Thank you so much guys!
Dan, sorry bout yesterdays thread...was caught at work hence couldnt reply back...Thanks for the concern.

And here is one more code...error is happening in blue line...

Code:
Sub Macro7()
'
' Macro7 Macro
'
'
    Sheets("Transport").Select
    Range("E12:F17").Select
    With Selection.Validation
        .Delete
    End With
    Range("E12:F17").Select
   [COLOR=blue]Selection.NumberFormat = "h:mm;@"[/COLOR]
    Range("A11").Select
End Sub


Runtime error 1004<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
Unable to set numberformat property of the range class<o:p></o:p>
 
Last edited:
Upvote 0
Just to provide another possible example for using the Worksheet_Change event for multiple ranges with different results.

Code:
Private Sub Worksheet_Change(ByVal target As Range)
    Application.EnableEvents = False
        If Not Intersect(target, Range("C4")) Is Nothing Then
            Call CheckBoxReset
            Call AverageFive
            Call PROIMain
            Call BorderPROI
            Sheets("Client Information").Select
        End If
        If Not Intersect(target, Range("C8")) Is Nothing Then
            Call CheckBoxReset
            Call AverageFive
        End If
        If Not Intersect(target, Range("C5, C8, C10")) Is Nothing Then
            Call ParticipationDate
        End If
    Application.EnableEvents = True
End Sub

As far as your other macro... it shouldn't be erroring on that line... it works fine on my end. Is that range and sheet protected? And here is your code cleaned up a bit :biggrin:

Code:
Sub Macro7()
'
' Macro7 Macro
'
'
    With Sheets("Transport")
      .Range("E12:F17").Validation.Delete
      .Range("E12:F17").NumberFormat = "h:mm;@"
      .Range("A11").Select
    End With
End Sub
 
Upvote 0
Pedie,
No problem about yesterday. I was pretty tied up myself.
As for the formatting code, I too have no problem running it without error.
(And MrKowz is showing you what I was going to to clean it up.)

:)
 
Upvote 0
I also can run it without problem but when the sheet is locked.

i used the code sheet1.unprotect password:-"...."
but still it gives error....not sure what was the problem that it is giving error:confused::)
 
Upvote 0
I hate to ask the obvious... but is Sheet1 the sheet named 'Transport'?

What happens if you run:
Sheets("Transport").Unprotect "...." ?
 
Upvote 0
Dan, I think it is working now....:biggrin:
Thank you soo much...I know you're busy but you're still here trying to help me everynow and then since Peter and Brian is not around these days.....:biggrin:

Dan, thanks alot!

I wonder where are these guys..!:biggrin:
 
Upvote 0

Forum statistics

Threads
1,223,268
Messages
6,171,100
Members
452,379
Latest member
IainTru

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