Reputation: 275
I am trying to make it so that when one of the specific cells in my code is changed then it will display a message but i am getting the following error message "run time error 438 object doesnt suport this property or method". Not realy sure what this means. could someone please help me understand. Here is the code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Adress = "F48,I48,L48,F50,I50,L50,I52,L52,N52" Then
MsgBox "You are about to change an AP-42 Emision Factor"
End If
End Sub
Upvotes: 1
Views: 926
Reputation: 275
The Code Siddharth Gave works wonders so Thanks so much. I was running into trouple because I was trying to make two Worksheet_Change events by writing the code the following way:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$D$8" Then
Toggle_Rows
End If
End Sub
Private Sub Worksheet_Change(Byval Target As Range)
If Not Intersect(Target, Range("F48,I48,L48,F50,I50,L50,I52,L52,N52")) Is Nothing Then
MsgBox "You are about to change an AP-42 Emision Factor"
End If
End Sub
As you can imagine this did not work, gave me an Ambiguous name error. So after some research the following is the way two write these two functions as one:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$D$8" Then
Toggle_Rows
End If
If Not Intersect(Target, Range("F48,I48,L48,F50,I50,L50,I52,L52,N52")) Is Nothing Then
MsgBox "You are about to change an AP-42 Emision Factor"
End If
End Sub
Thanks for all the help everyone!
Upvotes: 1
Reputation: 149305
Is this what you are trying?
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("F48,I48,L48,F50,I50,L50,I52,L52,N52")) Is Nothing Then
MsgBox "You are about to change an AP-42 Emision Factor"
End If
End Sub
Worth Reading: MS Excel crashes when vba code runs
Upvotes: 1
Reputation: 19067
1st, as mentioned in comment, use Target.Address
which is correct property name.
2nd, your if
statement will never return true. Target.Address
will always return something like this: $E$2
, $E$3:$E$4
, and so on... In your situation you should use something like Intersect
or Union
methods.
Edited- possible solution using Union
method:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngTMP As Range
Set rngTMP = Range("F48,I48,L48,F50,I50,L50,I52,L52,N52")
If Union(Target, rngTMP).Address = Union(rngTMP, rngTMP).Address Then
MsgBox "Ok"
End If
End Sub
Upvotes: 2