简体   繁体   中英

438 Run Time error

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

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

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM