简体   繁体   中英

Excel formula to change the value of another cell?

is it possible to force an [UDF] user-defined-function to return result on a cell we specify ? in another words : if I execute an UDF in the cell A1 it will return result in A2 cell , but what if I want to change A2 by another cell ?

Not directly.......a UDF will only return a value to the cell in which it resides. You could include a Calculate event macro in the worksheet. The Calculate macro could respond to the value that the UDF returns to A1 and change A2 accordingly.

Another approach is to have the UDF return an array and place the UDF to span both A1 and A2

EDIT:

Here is a very simple example of getting a UDF to fill more than one cell.

In B1, enter: 7

Install the following UDF:

Public Function circlee(r As Double)
    Dim ary(1 To 3, 1 To 1) As Double
    ary(1, 1) = 2 * r
    ary(2, 1) = WorksheetFunction.Pi() * 2 * r
    ary(3, 1) = WorksheetFunction.Pi() * r * r
    circlee = ary
End Function

Then hi-light cells A1 thru A3 and type the array formula:

=circlee(B1)

Because it is an array formula, it must be entered with CNTRL-SHFT-ENTER rather than just the ENTER key.

You should see in cells A1 thru A3 the diameter, circumference and area of a circle whose radius is in B1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM