VBA替换不适用于超过1.0的数字

时间:2017-12-07 16:08:32

标签: excel-vba replace vba excel

我试图创建一个获取所选单元格的宏,替换"。"使用",",然后将其更改为double,将其除以2并将其四舍五入。

细胞的例子:

0.910 1.000

For Each Cell In Selection
    Cell.Value = Replace(Cell, ".", ",")
    Cell = CDbl(Cell.Value)
    Cell = Cell / 2
    Cell = Round(Cell, 4)
Next Cell

我期待得到:

0.4550 0.5000

问题在于,如果我的数字大于1.0,则只删除"。"并且不会用任何东西替换它,因为单元格有3位小数,我突然得到1000而不是1。

所以我得到了:

0.4550 500

对出现问题的任何建议?

2 个答案:

答案 0 :(得分:0)

怎么样:

Sub foo2()
Dim cell As Range
Dim value As Double
    For Each cell In Selection
        cell = Application.RoundUp(cell, 1)
        cell = CStr(cell.value)
        cell.NumberFormat = "@"
        If cell.value <> "1" Then
            cell.value = cell.value & "00"
        Else
            cell.value = cell.value & ",000"
        End If
        cell.value = Replace(cell.value, ".", ",")
    Next cell
End Sub

答案 1 :(得分:0)

有点简单;记入@njimack

For Each cell In Selection
    If IsNumeric(cell) Then cell.Formula = Round(cell.Value, 1)
Next cell