How to identify if a named range exists

Ken Puls

Active Member
Joined
Jun 9, 2003
Messages
484
Hello,

I'm writing code to insert a new required worksheet in many of my current workbooks. A couple of the cells have formulas in them that refer to a named formula. I need to make sure that the formula exists in the current workbook, but am unsure of how to code it.

What I need is to have the following:

If activeworkbook.range.name("date") exists Then
Else msgbox("Your workbook does not contain a named date range!")
Exit sub
End if

Can anyone help?

Thanks,
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hello Ken, try the following:

<font face=Courier New><SPAN style="color:darkblue">Sub</SPAN> sdjhfjhsfjdshfjdshjfkhdsjkhfsjkhfjkshdfkjfdhjkshfjkhfjkhsdjh()
<SPAN style="color:darkblue">Dim</SPAN> rng <SPAN style="color:darkblue">As</SPAN> Range
<SPAN style="color:darkblue">On</SPAN> <SPAN style="color:darkblue">Error</SPAN> <SPAN style="color:darkblue">GoTo</SPAN> 1
<SPAN style="color:darkblue">Set</SPAN> rng = Range("date")
<SPAN style="color:darkblue">On</SPAN> <SPAN style="color:darkblue">Error</SPAN> <SPAN style="color:darkblue">GoTo</SPAN> 0

<SPAN style="color:darkblue">Set</SPAN> rng = <SPAN style="color:darkblue">Nothing</SPAN>

<SPAN style="color:darkblue">Exit</SPAN> <SPAN style="color:darkblue">Sub</SPAN>

1:
MsgBox "Your workbook does not contain a named date range!"

<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">Sub</SPAN></FONT>
 
Upvote 0
Beautiful, thanks!

Where did you learn this stuff, anyway? I'm trying to find a course on VBA, specifically on the Excel object model, but can't find anything out there.

Cheers,
 
Upvote 0
You're welcome.

I loiter on Excel message boards. :lol: Some truth to that, and I've done a lot of reading on the internet, help files. Took a beginner class at one point. Don't know where in your neighborhood you go for a more advanced one...

You pick it up as you go, at some point it became apparent to me that if you try to set an object, and that target object doesn't exist, vb's gonna crap out, and you can use this to your advantage when handled correctly.

Bon chance eh.
 
Upvote 0
Ken,

John Walkenbach's Excel 2000 Power Programming with VBA is excellent.

You can find it on Amazon.com for less than $30.

And what I've picked up here has helped immensly too!

Smitty
 
Upvote 0
Nate, you must be older than your picture looks to have learned that much from help files :lol:

Is that picture from the Rosedale yearbook? :laugh: (just kiddin!)

Thanks again...
 
Upvote 0
Upvote 0
Combined ... Better?

Code:
Public Function xExists(Optional wbName As String, Optional shName As String, Optional rngName As String) As Boolean
    'returns TRUE if the range/sheet/book exists,
    'returns FALSE if the range/sheet/book is invalid
    
    If wbName = "" Then wbName = ActiveWorkbook.Name
    If shName = "" Then shName = ActiveSheet.Name
    If rngName = "" Then rngName = "A1"

    On Error GoTo Invalid
    xExists = False
    Test_Exist = Workbooks(wbName).Sheets(shName).Range(rngName).Value
    xExists = True
    
Invalid:
    Err.Clear
    
End Function
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,623
Latest member
Techenthusiast

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top