Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Validate Named Range Exists

I need a little assistance. I have a workbook that has many named ranges
from single cell to multiple cells. As users do get in and play and screw
things up, I wanted to create a function that will validate a named range
exists before my code tries to go do something with that range. Is there a
way to do that?

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Validate Named Range Exists

Try something like the following code:


Function IsValidName(Nm As String) As Boolean

Dim N As Name
On Error Resume Next
Set N = ThisWorkbook.Names(Nm)
If N Is Nothing Then
IsValidName = False
Else
If InStr(1, N.RefersTo, "#REF") 0 Then
IsValidName = False
Else
IsValidName = True
End If
End If

End Function


You can then call this function from code like

If IsValidName("TheName") = True Then
' good name
Else
' bad name
End If


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"John Jost" wrote in message
...
I need a little assistance. I have a workbook that has many
named ranges
from single cell to multiple cells. As users do get in and
play and screw
things up, I wanted to create a function that will validate a
named range
exists before my code tries to go do something with that range.
Is there a
way to do that?



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default Validate Named Range Exists

This is a simple function to test for a named range... Probably an easier
way... but this works.


Function named_range_exists(nr As String) As Boolean
Dim rChk As Integer
named_range_exists = True
On Error Resume Next
rChk = ActiveSheet.Range(nr).Cells.Count
If Err.Number < 0 Then named_range_exists = False
On Error GoTo 0
End Function



"John Jost" wrote in message
...
I need a little assistance. I have a workbook that has many named ranges
from single cell to multiple cells. As users do get in and play and screw
things up, I wanted to create a function that will validate a named range
exists before my code tries to go do something with that range. Is there

a
way to do that?



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Validate Named Range Exists

John,

Sub TryNow()
Dim NameIsOK As Boolean
NameIsOK = GoodName("TestName")
If NameIsOK Then
MsgBox "It's good"
Else
MsgBox "It's missing"
End If
End Sub

Function GoodName(myName As String) As Boolean
Dim CurName As String

GoodName = False
On Error GoTo NoName
CurName = Range(myName).Name
GoodName = True
NoName:
End Function

HTH,
Bernie
MS Excel MVP


"John Jost" wrote in message
...
I need a little assistance. I have a workbook that has many named ranges
from single cell to multiple cells. As users do get in and play and screw
things up, I wanted to create a function that will validate a named range
exists before my code tries to go do something with that range. Is there a
way to do that?



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Validate Named Range Exists

Option Explicit
Function OkRangeName(myName As String) As Boolean
On Error Resume Next
OkRangeName = Not CBool(ActiveWorkbook.Names(myName).RefersToRange Is Nothing)
On Error GoTo 0
End Function

'I named a cell "hi" and didn't name a range "hithere"
'and tested with

Sub testme()
MsgBox OkRangeName("hi")
MsgBox OkRangeName("hithere")
End Sub

John Jost wrote:

I need a little assistance. I have a workbook that has many named ranges
from single cell to multiple cells. As users do get in and play and screw
things up, I wanted to create a function that will validate a named range
exists before my code tries to go do something with that range. Is there a
way to do that?


--

Dave Peterson


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Validate Named Range Exists

Wow, I'm very thankful for so many options from which to choose. Thanks one
and all.

"John Jost" wrote:

I need a little assistance. I have a workbook that has many named ranges
from single cell to multiple cells. As users do get in and play and screw
things up, I wanted to create a function that will validate a named range
exists before my code tries to go do something with that range. Is there a
way to do that?

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
inserting a named range into new cells based on a named cell Peter S. Excel Discussion (Misc queries) 1 June 4th 06 03:53 AM
Determining whether a named textbox on a chart exists P. Dua-Brown Excel Programming 4 October 20th 05 03:31 PM
If any cell in named range = 8 then shade named range JJ[_8_] Excel Programming 3 August 26th 05 11:09 PM
how to tell if a named range exists Gixxer_J_97[_2_] Excel Programming 2 June 1st 05 07:38 PM
easy way to test if a Named Range exists Andrew Bauer Excel Programming 4 July 10th 03 07:32 PM


All times are GMT +1. The time now is 05:42 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"