从单元格中取回公式 - VBA [英] Get back a formula from a cell - VBA

查看:163
本文介绍了从单元格中取回公式 - VBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个VBA代码,其中我需要从一张表中引用一个不同的表格
的公式,例如:在单元格(1,3)中的表A中有一个公式

 = R [-1] C * R [-2] C

我想在单元格(1,3)中的表B中放置公式

 = A!R [-1] C * A!R [-2] C

我想保存一个字符串只有公式 -

 = R [-1] C * R [-2] C

手动进行我需要的调整。



所以我的问题是:




  • 如何在单元格中获取公式?当我写 temp =工作表(A)。单元格(1,3)

    我得到从单元格中返回值,而不是公式...


解决方案

(在这种情况下,设置)单元格中的公式( A1 ),奇怪的是:

  Range(A1)。Formula 

(见这里详细信息)。



这只是一个字符串,所以你可以使用常规的VBA字符串函数来评估/操作它,如 left right mid instr 等等。



根据链接页面,为常量单元格返回的公式只是常量本身,空白单元格返回



您可以使用该信息来确定是否实际公式化。


I'm writing a VBA code, in which I need to reference in one sheet a formula from a different sheet for instance: in sheet A in cell (1,3) there's a formula

"=R[-1]C*R[-2]C"

and I want in sheet B in cell (1, 3) to put the formula

"=A!R[-1]C*A!R[-2]C"

I want to save in a string only the formula -

"=R[-1]C*R[-2]C"

And manually do the adjustments I need.

So my question is:

  • How can I get the formula in the cell? when I write "temp=worksheets("A").cells(1,3)",
    I get back the value from the cell, and not the formula...

解决方案

You can get (and set) the formula in a cell (A1 in this case) using, strangely enough:

Range("A1").Formula

(see here for details).

That just comes back as a string so you can evaluate/manipulate it with the regular VBA string functions like left, right, mid, instr and so on.

As per the linked page, the formula returned for a constant cell is just the constant itself, and a blank cell returns "".

You can use that information to decide if it's actually formulaic or not.

这篇关于从单元格中取回公式 - VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆