The Range NameExists function

This function accepts a single argument (a range name) and returns True if the range name exists in the active workbook:

Private Function RangeNameExists(nname) As Boolean ' Returns TRUE if the range name exists Dim n As Name RangeNameExists = False For Each n In ActiveWorkbook.Names

If UCase(n.Name) = UCase(nname) Then RangeNameExists = True Exit Function End If Next n End Function

Another way to write this function follows. This version attempts to create an object variable using the name. If doing so generates an error, then the name does not exist.

Private Function RangeNameExists2(nname) As Boolean ' Returns TRUE if the range name exists Dim n As Range On Error Resume Next Set n = Range(nname)

If Err.Number = 0 Then RangeNameExists2 = True _ Else RangeNameExists2 = False End Function

0 0

Post a comment