VBA十进制值

时间:2012-12-02 20:48:01

标签: excel vba

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

2 个答案:

答案 0 :(得分: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 

答案 1 :(得分: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