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