Bob said:
Hi Clif:
No I have not tried anything. For now, I guess, I am sticking to
using arrays to return multiple function values.
Bob
Bob, I learned something new about using events with Excel this week
(I've been using them in Access, but so far not in Excel) and in case it
might be useful for you I'll pass it along:
In an earlier reply I mentioned that events needed to be enabled --
turns out I was only partially correct. Workbook and worksheet events
are enabled by default, so are quite easy to use and require no class
module. Event procedures reside in either the workbook or the worksheet
(so far all my code has been placed in my PERSONAL.XLS workbook.)
From what I understand of your original post you want to use VBA code to
manipulate cells in a worksheet in response to a recalculation (that's
what I understand by your reference to a workbook function.)
Place this simple procedure in the workbook code module and it will pop
the msgbox whenever (after!) any sheet in the workbook is recalculated:
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
MsgBox "Calculate!", vbInformation
End Sub
If I'm understanding your situation correctly all you need to is call
your subroutine (it doesn't need to be a function) from this procedure.
For testing, I opened a new workbook, set A1 to =B1 and added the
SheetCalculate procedure. So far, so good.
It appears from your example that your function is recursive: ie, you
wish to be able to modify A4 when =2*A2^A3+A4^2+5 is evaluated.
As expected, adding Range("b1") = "A" to my test procedure
resulted in an infinite loop. This procedure demonstrates a simple test
for recursion:
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
MsgBox "Calculate!", vbInformation
If Range("b1") <> "A" Then
Range("b1") = "A"
MsgBox "Calculate2!", vbInformation
End If
End Sub
and worked as expected.
As I mentioned, this is a Workbook event procedure, and is called (by
Excel) anytime the Worksheet calculate event 'fires' for any worksheet
in the workbook. There are also Worksheet event procedures which are
called only when the worksheet they reside in fires the corresponding
event.
(VBA code is part of a workbook and/or worksheet template if you need
newly created worksheets or workbooks to have event handling.)
Here's the worksheet calculate procedure I used to solve my issue:
Private Sub Worksheet_Calculate()
' Speak Height calculation result
If ActiveCell.Address = "$B$4" Then
Range("E6:F6").Speak
End If
End Sub
(I was working through a stack of production drawings verifying the
math -- and it ocurred to me that Excel likely could speak the
calculation result which would save me the hassle of moving my eye focus
from the drawing to the screen & back for each calculation. Worked like
a charm!)
An Excel Help search for workbook object events or worksheet object
events gives you a good starting point for further reading.
An issue that will come up (and while I live with it I don't feel
comfortable with my level of knowledge) is macro security. I'm not
comfortable with setting Macro Security to Low (poor understanding of
the risks? Likely.) so my solution so far has been to add a digital
signature to any workbook that I have added code to. (I don't know if
templates will pass a digital signature along when a new copy is saved.)
I don't know if any of this will help you or not --- but I was so
excited about how well using events worked to solve my little issue, and
how easy it was to do that I just 'had' to share it with someone, and
your question came to mind.
So, fwiw, I pass this along.