View Single Post
  #14   Report Post  
Posted to microsoft.public.excel.programming
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default Evaluate a Name in Non-active WB

In my OP I gave a simplifed example of a Named formula I wanted to
'Evaluate', even if the Name is in a Non-active workbook. I forgot people
might use lateral thinking to come up with a solution based on my contrived
post! In hindsight I should have given the bigger picture with a real
example. So here it is, hope you are sitting comfortably:

I want to return a list of all loaded addins. Referencing the addins
collection can be quite slow, particularly the first time in an Excel
session. I prefer to avoid referring to it (.Addins) at all unless really
necessary, and certainly not in a wb load event to check (say) if some addin
is loaded (when it probably is). Further, the Addins collection only
contains addins in the addin manager, and not addins which may have been
loaded by other means.

OK, that's the objective, return a list of all loaded addins and avoid use
of the Addins collection. As it happens there is an old XLM function which
does just that, very efficiently. It returns a horizontal array which, AFAIK
like all arrays returned from XLM's, can only be accessed in full when used
in a Named formula, =DOCUMENTS(2)

The normal way to use this XLM is like this:
define a name (ctrl-F3)
Name: LoadedAddins
Refersto: =DOCUMENTS(2)

Select cells in a row, at least as many as there may be potentially loaded
addins, array enter =LoadedAddins

I could just accept that, return values of cells in the array formula in
cells hidden in my own xla. So far pretty good but by no means perfect. In
particular the named formula in cells requires a total recalc to update, not
what I want to do in a wb load event (before I forget, actually in an OnTime
macro after all wb's have loaded).

A better way would be to evaluate the named formula without touching cells
and/or requiring a recalc. Indeed this is possible along the lines
previously posted:
vArr = [named_XLM_formula_without_quotes]

Except, sadly, the above only appears to work if the workbook containing the
name is the activeworkbook.

Here's a revised example for testing, if anyone's still looking !

Sub EvalXLMtest()
Dim bActive As Boolean
Dim s As String
Dim v

With ThisWorkbook
.Names.Add "XLAs", "=DOCUMENTS(2)"
v = [XLAs]
.Names("XLAs").Delete
bActive = .Names.Parent Is ActiveWorkbook
End With

If IsArray(v) Then
If Not bActive Then
s = "SUCCESS, array returned in NON active wb"
Else
s = "array returned but in active wb"
End If
s = s & vbCr & v(1) & " total " & UBound(v)
Else
If IsError(v) Then v = CStr(v)
s = v & vbCr & "Active = " & bActive
End If
MsgBox s

' Notes
' If while testing there are no loaded addins,
' change the 2 in =DOCUMENTS(2) to 1 or 3

' In normal use the name would not be temporary, but best
' to recreate & delete in testing to ensure the name does not
' get duplicated in another workbook

End Sub

An even better alternative to evaluating the name would to return the array
directly from the XLM. I'm pretty sure that's not possible but would be
delighted to be proved wrong!
Nothing came out of this related thread -
http://tinyurl.com/2bwack
(except privately to Jon, blame KeepItCool for those square brackets <g)

Regards,
Peter T

pmbthornton at gmail dot com