Function CheckNamedRangeExists(NRTxt As String) As Boolean
Dim NR As Name
Dim NRName As String
Dim E As Long
Dim NRMatch As String
NRMatch = UCase(NRTxt)
For Each NR In ActiveWorkbook.Names
NRName = UCase(NR.Name)
E = InStr(NRName, "!")
If E > 0 Then NRName = Mid(NRName, E + 1, 100)
If NRName = NRMatch Then
CheckNamedRangeExists = True
Exit For
End If
Next NR
End Function
Sub IfNoNamedRangeCreatOn ()
Dim n As Range
Dim NameExist As Boolean
Dim rngName
For Each n In Range("Named RangeA ")
rngName = n
NameExist = Evaluate("IsRef(" & rngName & ")")
Debug.Print NameExist
If NameExist = False Then
ActiveCell.Name = (n.Value)
'Now need to go and edit the “Refers To” range of the added NameRange
End If
Next
End Sub
I'm a little confused. What are you referring to with "Named RangeA". Is that a list you created outside the actual named ranges? Actual Named Ranges cannot have a space in them.I have managed to get to the point of creating a Named Range for ANY name in “Named RangeA” list that DOESN’T have a named range created.
Are you manually adding new named ranges? Do you have a different macro doing that? Are other users creating these? Maybe you need to store a current list of named ranges in a special spot and then compare that list with the system named ranges; any system named ranges not on the saved list are new. I don't think Excel saves a Date/Time for each named range.With my sub below is there a way to call the Name Manger and edit specifically the NEWLY added Named Range.
Sub CheckForNamedRange()
Dim Nm As String
Dim Rng As Range
Dim NR As Name
Dim NamRng As Range
Dim A As String
Dim ShtStr As String
Dim X As Long
Dim RngStr As String
Dim Sht As Worksheet
Dim Msg As String
Dim Cel As Range
Dim NmList As String
Dim xArea As Range
Set Rng = Selection
Set Sht = ActiveSheet
On Error Resume Next
For Each NR In ActiveWorkbook.Names
For Each xArea In Rng.Areas
For Each Cel In xArea
If InStr(NmList, Cel.ListObject.Name) = 0 Then
Msg = Cel.ListObject.Name 'Table Name first
NmList = NmList & "," & Cel.ListObject.Name
End If
A = ""
A = NR.RefersToRange.Address(False, False)
If A <> "" Then
If InStr(NR.Name, "!") = 0 Then
If InStr(NR.RefersTo, Sht.Name) > 0 Then
Set NamRng = Range(A)
If Not Intersect(NamRng, Cel) Is Nothing Then
If InStr(NmList, NR.Name) = 0 Then
NmList = NmList & "," & NR.Name
If Msg <> "" Then
Msg = Msg & vbCrLf & NR.Name
Else
Msg = NR.Name
End If
End If
End If
End If
End If
End If
Next Cel
Next xArea
Next NR
If Msg <> "" Then
MsgBox Msg, vbOKOnly, "Selection belongs to These Named Ranges"
Else
MsgBox "None", vbOKOnly, "Selection belongs to These Named Ranges"
End If
On Error GoTo 0
End Sub
n.Select
Application.Dialogs(xlDialogNameManager).Show
Sub IfNoNamedRangeCreatOne ()
Dim n As Range
Dim NameExist As Boolean
Dim rngName
For Each n In Range("Named RangeA")
rngName = n
NameExist = Evaluate("IsRef(" & rngName & ")") 'returns True or False
Debug.Print NameExist 'only needed for testing purposes
If NameExist = False Then
ActiveCell.Name = (n.Value) 'new named range is created
n.Select
MsgBox "You have created a new named range;" & vbCrLf _
& " but need to edit the RefersTo range", vbOKOnly
Application.Dialogs(xlDialogNameManager).Show
End If
Next
End Sub