如何使用VBA将硬编码的浮点数值添加到公式中 [英] How to add hardcoded float number values to the formula using VBA
本文介绍了如何使用VBA将硬编码的浮点数值添加到公式中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下VBA代码不起作用:(我假设addvalue是有理数)
The folllowing VBA code doesn't work: (I assume that addedvalue is rational numer)
Dim addedvalue As Double
addedvalue = 1
ActiveCell.FormulaR1C1 = ActiveCell.FormulaR1C1 & "+" & CStr(addedvalue)
但是,这很好用:
ActiveCell.FormulaR1C1 = ActiveCell.FormulaR1C1 & "+" & 1
不幸的是,我必须将addvalue作为变量.
Unfortunately, I must have addedvalue as a variable.
我也在尝试 ActiveCell.FormulaR1C1 = ActiveCell.FormulaR1C1&"+"&增值
但这也行不通.
I was trying also
ActiveCell.FormulaR1C1 = ActiveCell.FormulaR1C1 & "+" & addedvalue
but it also doesn't work.
我的错误消息是:在此处输入图片描述
更新问题是为什么这不起作用:
UPDATE The problem is why this doesn't work:
Sub added()
Dim rational As Double
rational = 1.1
ActiveCell.FormulaR1C1 = ActiveCell.FormulaR1C1 & "+" & rational
End Sub
推荐答案
如果您未使用US-EN版本的Excel,则有两个简单的选择:
If you are not using a US-EN version of Excel, you have two easy options:
- 在将数字附加到公式之前,先将其格式化为US-EN格式,或者
- 以您自己的语言环境格式编写公式
The answer by Vityata gives an example of how to use option 1.
选项2类似于:
Sub added()
Dim rational As Double
rational = 1.1
ActiveCell.FormulaLocal = ActiveCell.FormulaLocal & "+" & rational
End Sub
这篇关于如何使用VBA将硬编码的浮点数值添加到公式中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文