2012-12-02 140 views
1

下面的例程将数字和数字前面的数字和数字替换为右侧的数字和数字,但当数字为十进制时显示错误1004。VBA十进制值

我该如何解决?

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 
+0

,难道你不想导致这里的加法?最好是,如果您可以给我们一个关于您的输入数据和预期输出的示例,请将它作为示例... :) – bonCodigo

回答

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 
+0

即使将cell.value更改为cell.formula,它也会连续显示错误。 问题是,Excel VBA写入cell.value =“= 2”但不是cell.value =“= 2,2” –

+0

测试它,它在我的最终加起来2和0.2或2和0.02 .. 。等等。 – bonCodigo

+0

什么意思,如何修改cell.value =“= 2,2” –

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