1

下面的例程将 activecell 整数替换为数字之前的数字和 = 以及右侧两列的数字,但当数字为十进制时显示错误 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 
4

2 回答 2

0
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 
于 2012-12-02T20:55:09.387 回答
0

要更新单元格公式,您需要使用其中一个.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
于 2012-12-03T05:19:57.877 回答