VBA十进制值

下面的例程用数字前面的数字和=来代替激活的单元整数,而右边的数字是两列,但是当数字是十进制时,显示错误1004。

我该如何解决?

Sub EnterEqual() Dim CellContent As Variant Dim cell As Variant For Each cell In Selection CellContent = cell.Value cell.ClearContents cell.Value = "=" & CellContent & "+" & "rc[2]" Next cell End Sub 

 Sub EnterEqual() Dim CellContent As Integer '-- not sure why you need to use variant here Dim cell As Range '-- this has to be a range object For Each cell In Selection CellContent = cell.Value cell.ClearContents '-- you may add this as a formula cell.formula = "=" & CellContent & "+" & "RC[2]" '-- or use the below formular1c1 cell.FormulaR1C1 = "=" & CellContent & " + " & "RC[2]" Next cell End Sub 

要更新单元格公式,您需要使用.Formula属性之一。 从你的评论bonCodigo,似乎你的本地数字格式使用小数点,所以使用这个

 Sub EnterEqual() Dim CellContent As Variant Dim cell As Range ' <=== change type For Each cell In Selection CellContent = cell.Value ' This get the result of any existing formula as a value. cell.ClearContents cell.FormulaR1C1Local = "=" & CellContent & "+" & "rc[2]" Next cell End Sub