Help: Question Regarding Name Function

  • Thread starter Thread starter Dustin Schofield
  • Start date Start date
D

Dustin Schofield

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
 
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.
 
Back
Top