View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default using relative named ranges directly in functions

Hi John,

In your function Add2, Range("Bval") is not relative to anything. For
interest try:

Function Add2()
Add2 = Range("A1val") + Range("Bval")(Application.Caller.Row, 1)
'or
'Add2 = Range("A1val") + Range("Bval").cells(Application.Caller.Row, 1)
End Function

I don't see any reason why this would be unsafe as such other than returning
the Application.Caller reference might be somewhat slower if used in many
cells calculated at the same time. Niek may be able to advise either way.

If your function does nothing more than as posted, why not use a named
formula:

Name: MySum
Refersto: =A1Val+Bval

and simply enter: =MySum

You should be able to use Mysum on any sheet in any cells, except in A1 and
Column B on the sheet where A1val & Bval are defined.

Regards,
Peter T

"Thunder" wrote in message
oups.com...
Niek,

Thanks for your suggestions.

I inserted Application.Volatile into the Add2() function.
I also inserted a MsgBox statement at the end of the function and
forced a recalculaton of the sheet.

The Add2() function gets called 4 times as expected, but on each call
it adds cell "B1" to "A1", instead of
using the cell in column B from the same row as it was called from i.e
B1, B2, B3, B4.

I was trying to make the function call simpler to avoid the
possibbility of referring to the wrong cell
when entering the formula into the cell, but I guess this is not the
way to do it.

Regards
John