'格式'VBA函数返回运行时错误424

时间:2019-10-20 16:47:49

标签: excel vba

我无法使用VBA中的格式功能。一切正常,直到我使用了格式。如果没有格式功能,下面的代码将为我提供正确的TNPS分数,但是我希望以百分比的形式显示格式,也只用2个小数点。因为它会产生错误运行时错误'424'对象为必需< / strong>,我无法继续前进。我已经提到了可能的重复条目:Getting error 424但无法解决。请帮忙。

Public Function tnps(promoter As Integer, passive As Integer, detractor As Integer)
tnps = (promoter - detractor) / (promoter + passive + detractor)
End Function


Sub main()

Dim pro As Integer
Dim pass As Integer
Dim det As Integer

For i = 2 To 25

pro = Cells(i, 2).Value
pass = Cells(i, 3).Value
det = Cells(i, 4).Value
Format(Cells(i, 5), "Percent") = tnps(pro, pass, det)

Next i
End Sub

2 个答案:

答案 0 :(得分:2)

Format应该用于“表达式”而不是单元格。

您应像这样使用Range.NumberFormat

Cells(i, 5) = tnps(pro, pass, det)
Range(Cells(i, 5)).NumberFormat = "0.00%"

答案 1 :(得分:1)

将以下代码更改为

Format(Cells(i,5),“ Percent”)= tnps(pro,pass,det)

Cells(i,5)= Format(tnps(pro,pass,det),“ 0.00%”)

它将起作用。