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

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

问题描述

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

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"

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

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.

所以我的问题是:

  • 如何获取单元格中的公式?当我写 "temp=worksheets("A").cells(1,3)",
    我从单元格中取回值,而不是公式...

推荐答案

奇怪的是,您可以使用以下方法获取(和设置)单元格(在本例中为A1)中的公式:

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

Range("A1").Formula

(请参阅此处 详情).

这只是作为一个字符串返回,因此您可以使用常规 VBA 字符串函数(如 leftrightmid)评估/操作它、instr 等.

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天全站免登陆