Excel:如何在一个单元格中声明变量,并在另一个单元格中定义它们?

发布于 2024-10-27 19:56:53 字数 273 浏览 0 评论 0原文

是否可以在一个单元格的公式中声明变量,然后在另一个单元格中定义变量,其中还必须显示第一个单元格的公式结果。

例子: 单元格 A1 中的公式:
(=x+y)。
单元格 A2 中的结果:
A1公式的结果必须显示在A2中。
两个 A1 公式变量的定义,f.ex。 “X=5”和“Y=3”也必须放在A2中。
结果:
A2:“=A1 的结果,提供 x=3 和 y=5”

在屏幕上,它看起来像这样:
(A1 = ??????) 和
(A2 = 8)

Is it possible to declare variables in a formula in one cell, and then define the variables in another cell, where also the result of the formulr from the first cell has to be presented.

EXAMPLE:
Formula in cell A1:
(=x+y).
Result in cell A2:
The result of the A1 formula has to be shown in A2.
The definitions of the two A1 formula variables, f.ex. "X=5" and "Y=3", has to be placed in A2 too.
RESULT:
A2: "=Result of A1, providing x=3 and y=5"

On the screen it would look something like this:
(A1 = ?????) and
(A2 = 8)

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

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

发布评论

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

评论(1

淑女气质 2024-11-03 19:56:53

您可以使用 VBA 将一个单元格 (A2) 作为文本写入另一个单元格 (A1) 中,从而定义该公式。基本思想是这样的:

Range("A2").Formula = "=" & Range("A1").Value

记住不要在A1中的公式前面写“=”,否则公式将在那里被计算。诀窍在于,这样做时公式将被视为普通文本。因此,在从 A1 中的文本中赋值时,必须将“=”放在公式前面。

[EDIT1]我弄错了你的问题。这是另一个尝试:

为了能够使用您在同一单元格中提供的“变量”作为结果,您必须:

  • 从 A2 中的文本中提取“变量”(例如 RegEx)
  • 替换变量名称在 A1 中使用 A2 中找到的变量值并将其存储在临时字符串变量 (formula_string) 中
  • 评估公式并根据您的模式设置 A2 的值/文本

    Range("A2").Value = "结果:" &评估(formula_string) & " using & Range("A2").Value

我这里没有 excel 来尝试,但我想你明白我的意思了。

[EDIT2]根据您的评论,再尝试一下:

如果您使用以下方法,您可以节省很多麻烦:

假设您有垂直的年份(从上到下)和水平的月份(从左到右)

  • 在矩阵左侧写下年份“数字” 在
  • 矩阵上方写下月份“数字”
  • 现在,您可以在一个单元格中编写公式,然后将其“拖动”到其他单元格,
    您只需“固定”月份的行和年份的列(您可以通过在要固定的维度前面写一个 $ 符号来实现这一点,

我希望这就是您想要的。

You can define a formula for one cell (A2) by writing it as a text in another cell (A1) by using VBA. The basic idea is this:

Range("A2").Formula = "=" & Range("A1").Value

Keep in mind not to write "=" in front of the Formula in A1, or the formula will be evaluated there. The trick is that the formula will be treated like normal text when doing so. Therefore the "=" has to be put in front of the formula when assigning it from the text in A1.

[EDIT1] I got your question wrong. Here's another try:

To be able to use "variables" you provide in the SAME cell as the result, you would have to:

  • Extract the "variables" from the text in A2 (e.g. RegEx)
  • Replace the variable-names in A1 with the values of the variables found in A2 and store that in a temporary string variable (formula_string)
  • Evaluate the formula and set the value/text of A2 according to your pattern

    Range("A2").Value = "Result: " & Evaluate(formula_string) & " using & Range("A2").Value

I have no excel here to try it out, but I think you get what I mean.

[EDIT2] Another try, according to your comment:

You can save yourself a lot of hassle if you use the following approach:

Let's say you have the years vertically (from top to bottom) and the months horizontally (from left to right)

  • Write the year "numbers" left of the matrix
  • Write the month "numbers" above the matrix
  • Now you can write the formula in one cell and just "drag" it along to the other cells,
    you just have to "fixate" the row for the month and the column for the year (You can do that by writing a $-Sign in front of the dimension you want to fixate

I hope that is what you wanted.

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