以下VBA代码不起作用: (我认为附加值是合理的数字)
Dim addedvalue As Double
addedvalue = 1
ActiveCell.FormulaR1C1 = ActiveCell.FormulaR1C1 & "+" & CStr(addedvalue)
然而,这个工作正常:
ActiveCell.FormulaR1C1 = ActiveCell.FormulaR1C1 & "+" & 1
不幸的是,我必须将addvalue作为变量。
我也在尝试
ActiveCell.FormulaR1C1 = ActiveCell.FormulaR1C1 & "+" & addedvalue
但它也行不通。
我的错误信息是: enter image description here
更新 问题是为什么这不起作用:
Sub added()
Dim rational As Double
rational = 1.1
ActiveCell.FormulaR1C1 = ActiveCell.FormulaR1C1 & "+" & rational
End Sub
答案 0 :(得分:0)
对于每个人来说,使用逗号作为小数分隔符,VBEditor有一个小的“功能”。长话短说,请尝试以下方法:
Sub TestMe()
Dim addedvalue As Double
addedvalue = 1.2
ActiveCell.FormulaR1C1 = ActiveCell.FormulaR1C1 & "+" & Replace(addedvalue, ",", ".")
End Sub
这是我的自定义功能,我用于这样的情况(并且它们非常多):
Public Function fnStrChangeCommas(ByVal myValue As Variant) As String
fnStrChangeCommas = Replace(CStr(myValue), ",", ".")
End Function
答案 1 :(得分:0)
尝试过并且工作正常:
Sub Test()
Dim addedValue As Integer
addedValue = 1
ActiveCell.FormulaR1C1 = ActiveCell.FormulaR1C1 & "+" & addedValue
End Sub
答案 2 :(得分:0)