VBA,如何使公式在Excel中可见
Dim n As Double n = Sh.Range("B10").Value Dim rate As Double rate = (datefin - datedepart) / n Cells(13, n + 3) = datefin For y = n - 1 To x Step -1 Cells(13, y + 3) = datefin - rate datefin = datefin - rate Next y
我有这个简单的for
循环,它将值从n
(由用户给出)并根据rate
(也由用户给出的值计算)后退到Excel中的单元格中。
我得到的输出值是正确的,但我想让我的公式在Excel中可见。 例如,如果我按下单元格D13
我希望显示我使用的公式。 我试图用ActiveCell.Formula
或ActiveCell.FormulaR1C1
函数做到这一点,但由于n
并不总是一个特定的数字(因此在一个特定的单元格),我找不到如何执行我想要的。
谢谢!!!
一般来说,你可以使用.Formula
将公式附加到单元格中,但是如果单个组件不是实际包含在电子表格的其他单元格中,那么最好的办法就是像
Cells(13, y + 3).Formula = "=" & format(datefin) & " - " & format(rate)
如果datefin是9并且rate是2,那么将公式"=9 - 2"
放入单元格中。