Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
替换整数值excel工作表_Excel_Vba - Fatal编程技术网

替换整数值excel工作表

替换整数值excel工作表,excel,vba,Excel,Vba,我正在用8替换excel中的整数7,6。然而,下面的代码并没有真正按照预期工作。它似乎代替了弦。我使用replace功能编写了一段类似的代码,但即使这样也没有产生我所期望的结果。我做错了什么 Sub MacroTest() ' Dim rng As Range, cell As Range Set rng = Sheets("Naomi").Range("H1:H10000") For Each cell In rng If cell > 0 Then cell = Wo

我正在用8替换excel中的整数7,6。然而,下面的代码并没有真正按照预期工作。它似乎代替了弦。我使用replace功能编写了一段类似的代码,但即使这样也没有产生我所期望的结果。我做错了什么

Sub MacroTest()
'
Dim rng As Range, cell As Range
Set rng = Sheets("Naomi").Range("H1:H10000")

For Each cell In rng
    If cell > 0 Then
    cell = WorksheetFunction.Substitute(cell, "7,6", "8")
    End If
Next

End Sub
感谢您的指导。

请使用:

If cell.Value = 7.6 Then
    cell.Value = 8
End If

我想桃色雀提供了最好的方法。
这回答了为什么它返回
String
而不是
Number

您已经发现,
Substitute
在WS中尝试时返回一个字符串。
尝试使用
Val
函数,然后将值转换为
数字,如下所示

With Application.WorksheetFunction
    For Each cell in rng
        If cell > 0 Then
            cell = Val(.Substitute(cell, 7.6, 8))
        End If
    Next
End With
或者您也可以使用
评估
,如下所示:

If cell > 0 Then
    cell = Evaluate("Value(Substitute(" & cell & ",7.6,8))")
End If
无需将
7.6
括起来。“

Substitute
接受数字作为参数