The problem is that I want the property Name.Name of the Target range in the Change event as the following Code show
Private Sub Worksheet_Change(ByVal Target As Range)
If Left(Target.Name.Name, 6) = "CritFA" Then
critFA (Target.Name.Name)
End If
End Sub
It works when the cell where I make a change has a Name or TagName (I don't know which is the right way to call them but those are the ones you can create from the Formula Bar), but when the cell doesn't have a TagName gives me Err 1004.
I already tried with isNull
, Is Nothing
, and almost everything but, it doesn't work.
You can try something like this:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim nm
on error resume next
nm = Target.Name.Name
on error goto 0
If Left(nm, 6) = "CritFA" Then
critFA (Target.Name.Name)
End If
End Sub
Accessing Range.Name
can raise an error if said Range
is not a named range. I think the scope of On Error Resume Next
can be limited to it alone.
This works for me:
Option Explicit
Private Function GetNameOrNothing(ByVal someRange As Range) As Name
On Error Resume Next
Set GetNameOrNothing = someRange.Name
On Error GoTo 0
End Function
Private Sub Worksheet_Change(ByVal Target As Range)
Dim targetName As Name
Set targetName = GetNameOrNothing(Target)
If targetName Is Nothing Then Exit Sub
If Left(targetName.Name, 6) = "CritFA" Then
critFA targetName.Name
End If
End Sub
This is what I did similar to what @Tim Williams suggest. It works and solves the problem but I don't feel so comfortable with an error in my code.
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Left(Target.Name.Name, 6) = "CritFA" Then
If Err.Number <> 1004 Then
critFA (Target.Name.Name)
End If
Err.Clear
End If
End Sub
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.