通过vba在Excel中添加单元格公式 [英] Add a cell formula in Excel via vba

查看:199
本文介绍了通过vba在Excel中添加单元格公式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是Excel或VBA专家,但是我想将这个当前的excel公式插入到使用VBA的单元格中。



当前Excel公式:

  = IF(OR(ISNUM(D570)= FALSE; ISNUM(D573)= FALSE);; IF(C573 =Total; D573-D570;))

VBA公式:

  ActiveSheet.Range(a& ActiveSheet.Rows.Count).End(xlUp).Offset(2,12).Value == IF(OR ISNUM(R [-3] C [-9])= FALSE; ISNUM(R [0] C [-9])= FALSE); ''; IF(R [0] C [-10] = '总计' ; R [0] C [-9] -R [-3] C [-9];''))

它不工作...有人可以帮我吗?

解决方案

尝试使用 .formula = 而不是您的VBA代码中的 .value =



设置单元格的 .value 只会以您指定的任何值复制。在这种情况下,您的公式只是转换为字符串值。



使用 .formula 属性实际上指定用来计算的值的公式,这是你正在寻找的。

I’m not an Excel or VBA expert but I want to insert this current excel formula into cell’s using VBA.

Current Excel formula:

=IF(OR(ISNUM(D570)=FALSE;ISNUM(D573)=FALSE);"";IF(C573="Total";D573-D570;""))

VBA formula :

 ActiveSheet.Range("a" & ActiveSheet.Rows.Count).End(xlUp).Offset(2, 12).Value = "=IF(OR(ISNUM(R[-3]C[-9])=FALSE;ISNUM(R[0]C[-9])=FALSE);'';IF(R[0]C[-10]='Total';R[0]C[-9]-R[-3]C[-9];''))"

It doesn’t work… Someone can help me please?

解决方案

Try using .formula = instead of .value = in your VBA code.

Setting the .value of a cell simply copies in whatever value you specify. In this case, your formula is simply converted to a string value.

Using the .formula property, you are actually specifying the formula that gets used to compute the value, which is what you are looking for.

这篇关于通过vba在Excel中添加单元格公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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