2012-12-02 144 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