ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Question Regarding Name Function (https://www.excelbanter.com/excel-programming/284423-re-question-regarding-name-function.html)

Tom Ogilvy

Question Regarding Name Function
 
It should work as you suggested, but perhaps the author was trying to
illustrate some point about making the function perform in both arenas where
there would be a difference.

--
Regards,
Tom Ogilvy

"Dustin Schofield" wrote in message
...
I am new to excel programming and so am using books in an effort to learn.
Up until now, I have attempted to completely understand presented code
before moving on. I have come across one example that is giving me a
difficult time, however.

It is a function that is apparently designed to run both as a worksheet
function and as a VBA callable procedure. It's purpose is to simply check
to see whether a name exists in a workbook and return a true or false
expression.

The code is as follows:


Function IsNameInWorkbook(stName As String) As Boolean

Dim X As String
Dim Rng As Range

Application.Volatile
On Error Resume Next

Set Rng = Application.Caller
Err.Clear

If Rng Is Nothing Then
X = ActiveWorkbook.Names(stName).Name
Else
X = Rng.Parent.Parent.Names(stName).Name
End If

If Err.Number = 0 Then IsNameInWorkbook = True

End Function


I follow this code for the most part but am wondering why certain elements
are necessary. My question is this: Why is a check performed to ascertain
where the function was called? Would not the code below work just as
effectively in both situations (that is - from a worksheet or VBA
procedure)?


Function IsNameInWorkbook(stName As String) As Boolean

Dim X As String

Application.Volatile
On Error Resume Next

X = ActiveWorkbook.Names(stName).Name

If Err.Number = 0 Then IsNameInWorkbook = True

End Function


Any help or thoughts would be greatly appreciated,

Thanx

Dustin






All times are GMT +1. The time now is 07:41 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com