Excel - 工作表作为函数

发布于 2024-12-08 04:03:16 字数 422 浏览 4 评论 0原文

我有两张 Excel 表格。第一个包含用于使用一个输入单元格 (A1) 和一个输出单元格 (B1) 进行计算的公式。 B1 的公式可以是 B1 = A1 * 3(示例)。

第二张表在 A 列中包含各种值:A1 = 4; A2=9; A3 = 5 ... 在此表的相应 B 列中,我想为每个 A(第二张表)输入值获取 B1(第一张表)= A1(第二张表)* 3 的结果。

基本上我想将第一个工作表视为一个函数,其中 A1 是参数,B1 是传递回第二个工作表的 B 列的结果。

表 2

A1 4      B1 12 (result from sheet 1)  
A2 9      B2 27 (result from sheet 1)

...

没有宏可以吗?

I have two excel sheets. The first contains a formula for calculation with one input cell (A1), and one output cell (B1). The formula for B1 could be B1 = A1 * 3 (example).

The second sheet contains various values in column A: A1 = 4; A2 = 9; A3 = 5 ... In corresponding column B of this sheet I'd like to get the result of B1 (first sheet) = A1 (second sheet) * 3 for each A (second sheet) input value.

Basically I'd like to treat the first sheet as a function, where A1 is the argument and B1 the result that is passed back to the second sheet's B column.

Sheet 2

A1 4      B1 12 (result from sheet 1)  
A2 9      B2 27 (result from sheet 1)

...

Is it possible without macros?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

表情可笑 2024-12-15 04:03:16

这是内置于 Excel 中的。在版本 2003 中,使用“数据”、“表格”菜单。
您可以在网上找到很多示例。这是 一个
您可以创建包含 1 或 2 个条目(参数)的此类表。

This is built into Excel. In version 2003, use the Data, Table menu.
You can find many examples on the net. Here is one.
You can create such tables with either 1 or 2 entries (parameters).

梦里泪两行 2024-12-15 04:03:16

我不这么认为......
如果在 B1 Sheet1 中有
3*A1

如果你在 Sheet2 B1 中尝试这个,

`=SUBSTITUTE(Sheet1!$B$1,"A1",A1)`

它会给出
3*4,Sheet2 B2 将是
3*9etc

但我不明白如何用公式将其强制为数字计算,而无需进行一些繁重的公式字符串解析来将数字与运算符分开(这不太可能根据需要进行弯曲,如果您更改了 B1 表 1) 中的条目

[更新 2: 但 fwiw 我已经使用命名范围完成了]

我使用了此范围名称

RngTest

=EVALUATE(3*INDIRECT("rc[-1]",FALSE))

这是一个全局范围名称,因此它适用于任何工作表,比我的功能更强大之前的 OFFSET 努力。它将左边的单元格乘以 3,

因此在 B1:B3 中输入 =RngTest(然后在这个新示例中输入 C1:C3)
给出你想要的输出
在此处输入图像描述

I don't think so .....
If in B1 Sheet1 you have
3*A1

If you try this in Sheet2 B1

`=SUBSTITUTE(Sheet1!$B$1,"A1",A1)`

it will give
3*4, and Sheet2 B2 will be
3*9etc

But I don't see how you could coerce this to a numberic calculation with formulae without possibly some heavy duty formula string parsing to separate numbers from operators (which is unlikley to flex as desired if you change the entry in B1 Sheet 1)

[Update 2: but fwiw I have done it with a named range]

I used this range name

RngTest

=EVALUATE(3*INDIRECT("rc[-1]",FALSE))

This is a global range name so it will work on any sheet, more powerful than my prior OFFSET effort. It multiplies the cell to the immediate left by 3

so entering =RngTest in B1:B3 (and then in this new example C1:C3 as well)
gives the output you want
enter image description here

無心 2024-12-15 04:03:16

我想你想在你的工作表第二列中使用它。

Sheet1!B1 * Sheet2!A1

I think you want to use this in your sheet two column.

Sheet1!B1 * Sheet2!A1
°如果伤别离去 2024-12-15 04:03:16

完全没有 VBA:预计会有很多痛苦,我不会去那里。但是......

为了大大减少痛苦,您实际上可以使用这个微小的 VBA 用户定义函数(技术上不是“宏”),基本上只是一个包装器,使 VBA 的 Evaluate 函数可用在工作表公式中:

Function eval(myFormula As String)
    eval = Application.Evaluate(myFormula)
End Function

然后您可以在工作表 2 的单元格 B1 中像这样使用它:

=eval(SUBSTITUTE(Sheet1!$B$1,"A1","A"&ROW()))

请注意,这要求工作表 1 单元格 B1 包含 A1*3 而不是 =A1*3,即没有等号。也许稍微多一些摆弄,即使在那里使用 = 符号也可以使其工作...

编辑:实际上,如果在输入公式之前将工作表 1 单元格 B1 格式化为文本,即使您的公式开始,这也将起作用带有 =

Entirely without VBA: expect lots of pain, I won't go there. But...

To substantially reduce the amount of pain, you could actually use this one tiny VBA user-defined function (not technically a "macro"), basically just a wrapper to make VBA's Evaluate function available in worksheet formulas:

Function eval(myFormula As String)
    eval = Application.Evaluate(myFormula)
End Function

You could then use it like this in cell B1 on sheet 2:

=eval(SUBSTITUTE(Sheet1!$B$1,"A1","A"&ROW()))

Note that this requires Sheet 1 cell B1 to contain A1*3 and not =A1*3, i.e. no equal sign. Maybe with a bit more fiddling around, it can be made to work even with the = sign there...

EDIT: Actually, if you format Sheet 1 cell B1 as Text before typing in the formula, this will work even if your formula starts with a =.

亢潮 2024-12-15 04:03:16

没有宏可以吗?

是的!

您现在可以使用 =LAMBDA 来实现此目的。

使用名称管理器定义您的函数,然后在第二个工作表的公式中引用它。

请参阅LAMBDA 函数简介中的语法。

有关如何使用 LAMBDA 函数的详细信息,请参阅 支持文档

Is it possible without macros?

Yes!

You can now use =LAMBDA for this.

Define your function using the name manager, then reference it in your second sheet's formula.

See syntax at Introducing the LAMBDA function.

For more information about how to use the LAMBDA function, see the support documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文