Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
easy way to test if a Named Range exists
Is there an easier way to test if a Named Range exists
than this loop: 'a long-winded way to check if "ToolVersion" exists... fnd = False For Each x In ActiveWorkbook.Names If x.Name = "ToolVersion" Then fnd = True Exit For End If Next x I need something like this: if ActiveWorkbook.Names.Item("x").Exists then ... |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
easy way to test if a Named Range exists
Andrew,
Try a function like Function Name(What As String, _ Optional WB As Workbook) As Boolean Dim N As Long On Error Resume Next N = Len(IIf(WB Is Nothing, ThisWorkbook, WB).Names(WhatName).Name) NameExists = (Err.Number = 0) End Function Then, you can call this with code like If NameExists("SomeName") = True Then ' name exists Else ' name does not exist End If -- Cordially, Chip Pearson Microsoft MVP - Excel Pearson Software Consulting, LLC www.cpearson.com "Andrew Bauer" wrote in message ... Is there an easier way to test if a Named Range exists than this loop: 'a long-winded way to check if "ToolVersion" exists... fnd = False For Each x In ActiveWorkbook.Names If x.Name = "ToolVersion" Then fnd = True Exit For End If Next x I need something like this: if ActiveWorkbook.Names.Item("x").Exists then ... |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
easy way to test if a Named Range exists
I get it: Suppress errors and try the name. Return True
if no error appeared. I guessed Microsoft forgot to implement a test like NameExists. Thanks! -----Original Message----- Andrew, Try a function like Function Name(What As String, _ Optional WB As Workbook) As Boolean Dim N As Long On Error Resume Next N = Len(IIf(WB Is Nothing, ThisWorkbook, WB).Names (WhatName).Name) NameExists = (Err.Number = 0) End Function Then, you can call this with code like If NameExists("SomeName") = True Then ' name exists Else ' name does not exist End If -- Cordially, Chip Pearson Microsoft MVP - Excel Pearson Software Consulting, LLC www.cpearson.com "Andrew Bauer" wrote in message ... Is there an easier way to test if a Named Range exists than this loop: 'a long-winded way to check if "ToolVersion" exists... fnd = False For Each x In ActiveWorkbook.Names If x.Name = "ToolVersion" Then fnd = True Exit For End If Next x I need something like this: if ActiveWorkbook.Names.Item("x").Exists then ... . |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
easy way to test if a Named Range exists
Looks like you are all set, but just be aware that a workbook name could
refer to a Constant, Formula, or a Named Range. Sub demo() ActiveWorkbook.Names.Add _ Name:="pi", _ RefersTo:="=3.14159" End Sub I am guessing from your Subject line that you are testing if it refers to a "Range." If so, you may want to also include "RefersToRange" Names("pi").RefersToRange ...etc -- Dana DeLouis Windows XP & Office XP = = = = = = = = = = = = = = = = = "Andrew Bauer" wrote in message ... I get it: Suppress errors and try the name. Return True if no error appeared. I guessed Microsoft forgot to implement a test like NameExists. Thanks! -----Original Message----- Andrew, Try a function like Function Name(What As String, _ Optional WB As Workbook) As Boolean Dim N As Long On Error Resume Next N = Len(IIf(WB Is Nothing, ThisWorkbook, WB).Names (WhatName).Name) NameExists = (Err.Number = 0) End Function Then, you can call this with code like If NameExists("SomeName") = True Then ' name exists Else ' name does not exist End If -- Cordially, Chip Pearson Microsoft MVP - Excel Pearson Software Consulting, LLC www.cpearson.com "Andrew Bauer" wrote in message ... Is there an easier way to test if a Named Range exists than this loop: 'a long-winded way to check if "ToolVersion" exists... fnd = False For Each x In ActiveWorkbook.Names If x.Name = "ToolVersion" Then fnd = True Exit For End If Next x I need something like this: if ActiveWorkbook.Names.Item("x").Exists then ... . |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
how to lookup if value exists in a range of data? | Excel Worksheet Functions | |||
Logical Test for a value in a named list? | Excel Discussion (Misc queries) | |||
Test if file exists | Excel Discussion (Misc queries) | |||
Named Cell test | Excel Discussion (Misc queries) | |||
inserting a named range into new cells based on a named cell | Excel Discussion (Misc queries) |