Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Referring to "previous cell" in a formula in a "varying current ce

I need to calculate a value using a formula that uses the reference to a
previous cell (e-g. "A10", when the formula used is for computing "A11"; in
the next row "A11" needs to be used for calculating "A12" and so on...), when
the cell in which the formula to be used keeps varying. For example, I need
to compute today's gain or loss compared to the previous day's, on a daily
basis in real time. Is there any way to do this in Excel 2003? In other
words, can I use the equivalent of "contents of cell A(i)" in the formula
contained in cell "A(i+1)", as could be used in any programming language?
Thanks for all, who could provide me an answer.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Referring to "previous cell" in a formula in a "varying current ce

Using simply a formula, you could use

=IF(COLUMN()=1,NA(),INDIRECT(ADDRESS(ROW(),COLUMN( )-1)))

This returns the value in the column to left of the cell in which the
formula appears. If the formula is in column A, there is nothing to
the left, so the result is #N/A.

In code, you could do something like the followng.

Function Test(D As Double) As Variant
On Error Resume Next
Test = D * Application.Caller.Offset(0, -1).Value
If Err.Number < 0 Then
Test = CVErr(xlErrNull)
End If
End Function

This uses Application.Caller to get the reference to the cell that
called Test, then uses Offset to get the value that is one column to
the left to the cell that called it.

Note, though, that since the cell to the left of Application.Caller
(which is the cell from which this function was called) is not a
direct parameter to Test, Excel will not automatically recalculate the
function if the value to the left is changed. E.g, if the function
Test appears in D1 as =TEST(A1), the result is A1*C1. However, if you
change C1, Excel does not recognize this as a change that affects the
TEST function, so it will not recalculate the result.

As a general rule, all inputs to a function written in VBA that will
be called from a worksheet cell should be passed in as a parameter.
Otherwise, you may get inconsistent results. E.g, a function like

Function ABC() As Double
ABC = Range("A1").Value * 10
End Function

is bady written because Excel won't recalc this function when A1 is
changed because Excel has no knowledge of what ABC does. Instead, you
would want to write

Function ABC(X As Double) As Double
ABC = X * 10
End Function

This will ensure that when X is changed, ABC will be recalculated.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)





On Thu, 10 Sep 2009 09:22:03 -0700, arasan77
wrote:

I need to calculate a value using a formula that uses the reference to a
previous cell (e-g. "A10", when the formula used is for computing "A11"; in
the next row "A11" needs to be used for calculating "A12" and so on...), when
the cell in which the formula to be used keeps varying. For example, I need
to compute today's gain or loss compared to the previous day's, on a daily
basis in real time. Is there any way to do this in Excel 2003? In other
words, can I use the equivalent of "contents of cell A(i)" in the formula
contained in cell "A(i+1)", as could be used in any programming language?
Thanks for all, who could provide me an answer.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,355
Default Referring to "previous cell" in a formula in a "varying current ce

Try recording a macro when you've entered the correct formula in a cell and
see what you get.

"arasan77" wrote:

I need to calculate a value using a formula that uses the reference to a
previous cell (e-g. "A10", when the formula used is for computing "A11"; in
the next row "A11" needs to be used for calculating "A12" and so on...), when
the cell in which the formula to be used keeps varying. For example, I need
to compute today's gain or loss compared to the previous day's, on a daily
basis in real time. Is there any way to do this in Excel 2003? In other
words, can I use the equivalent of "contents of cell A(i)" in the formula
contained in cell "A(i+1)", as could be used in any programming language?
Thanks for all, who could provide me an answer.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel - Golf - how to display "-2" as "2 Under" or "4"as "+4" or "4 Over" in a calculation cell Steve Kay Excel Discussion (Misc queries) 2 August 8th 08 01:54 AM
keyboard shortcut to return to previous cell after "find" or "got. Nadavb New Users to Excel 1 May 25th 08 01:39 AM
Functions for "current" & "previous" month to calculate data Priss Excel Worksheet Functions 11 April 15th 08 06:24 PM
If changed array formula reduce ""\""\""\ - signs to #Missing, will it make ... Maria J-son[_2_] Excel Programming 2 March 5th 06 12:20 PM
Complex if test program possible? If "value" "value", paste "value" in another cell? jseabold Excel Discussion (Misc queries) 1 January 30th 06 10:01 PM


All times are GMT +1. The time now is 10:28 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"