Is it possible to use VLOOKUP to return a formula?

别说谁变了你拦得住时间么 提交于 2021-02-08 07:32:05

问题


I have a table in an excel 2010 workbook (result_master.xls). Column A has Name, Column B has a Formula see example below

In another workbook(user_result_sheet.xls) I have a table where I want to use a VLOOKUP to return the appropriate formula in cell $B$2 from the table array in the other workbook based on lookup value from cell $A$1

The formulas in result_master.xls will change over time, so I wanted to be able to update them in one place rather than update the formulas in each of the 60+ user_results_sheet workbooks that will be distributed all over the company network.

my 1st attempt

=VLOOKUP($A$1,[result_master.xlsx]Sheet1!$A$1:$B$4,2,FALSE)

returned 0(zero), as it applies the formula to the (results_master) workbook

I need it to apply the formula to the (user_result_sheet) workbook

I tried using different combinations of LEFT,MID, and TRIM to remove the reference to the workbook/sheetname but even when that returned the correct part of the formula, it was as a string and then wouldn't calculate.


回答1:


You could write a single VBA user defined function:

Function Eval(s As String) As Variant
    Application.Volatile
    Eval = Application.Evaluate(s)
End Function

then use it like this:

In Sheet1 (note the absence of the leading equals sign):

Then in Sheet2:

With the formula =eval(VLOOKUP(Sheet2!A4,Sheet1!$A$1:$B$4,2,FALSE)) entered into B4 and copied down.




回答2:


I would use Named Formulas (Defined Names) to store your formulas in one place and then use CHOOSE to select which named formula to return.
CHOOSE uses a number as its first argument so you could use MATCH to convert your formula word choice to a number.



来源:https://stackoverflow.com/questions/32656304/is-it-possible-to-use-vlookup-to-return-a-formula

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!