How to plug a range object into a formula

跟風遠走 提交于 2020-02-15 08:07:19

问题


I want to plug a range object into a formula. An example should look roughly like this:

Dim x As Range
Set x = Range(Cells(1, 1), Cells(2, 1))

Range("C1").Formula = "=SUM(" & x & ")"

The result should be "=SUM(A1:A2)" in cell C1.

The point is to plug the the range object into a formula. I used SUM as an example, the real formula is more complicated.

I guess there is an easy answer to this, e.g. some method for a range object, but I have't found it after some pondering ...

Thanks in advance, Dainis


回答1:


You need

Range("C1").Formula = "=SUM(" & x.Address(False, False) & ")"

See also

http://www.dailydoseofexcel.com/archives/2004/04/16/worksheet-formulas-in-vba-part-i/

http://www.dailydoseofexcel.com/archives/2004/04/16/worksheet-formula-in-vba-part-ii/




回答2:


Use x.address, that should do the trick



来源:https://stackoverflow.com/questions/2288554/how-to-plug-a-range-object-into-a-formula

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