Excel:如何在一个单元格中声明变量,并在另一个单元格中定义它们?
是否可以在一个单元格的公式中声明变量,然后在另一个单元格中定义变量,其中还必须显示第一个单元格的公式结果。
例子: 单元格 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 VBA 将一个单元格 (A2) 作为文本写入另一个单元格 (A1) 中,从而定义该公式。基本思想是这样的:
记住不要在A1中的公式前面写“=”,否则公式将在那里被计算。诀窍在于,这样做时公式将被视为普通文本。因此,在从 A1 中的文本中赋值时,必须将“=”放在公式前面。
[EDIT1]我弄错了你的问题。这是另一个尝试:
为了能够使用您在同一单元格中提供的“变量”作为结果,您必须:
评估公式并根据您的模式设置 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:
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:
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)
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.