我目前正在尝试创建一个从0到10之间的分数列表中计算净推荐值得分的函数。您可以使用此公式(我尝试编码的公式)计算NPS:
所以我在下面创建了这个代码(使用Rng作为我在编写公式时选择的单元格范围),这似乎不起作用。我已经定义了每个值,但仍然会出现#VALUE错误。有人可以解释一下我的错误在哪里吗?
Function NPSCOMPUTE(Rng As Range)
Dim Cell As Range
Dim NPS As Integer
Dim PourcentagePromoters As Integer
Dim PourcentageDetractors As Integer
Dim Total As Integer
Dim Promoters As Integer
Dim Detractors As Integer
Range("D1").Value = "Net Promotor Score"
Detractors = 0
Promoters = 0
Total = WorksheetFunction.Sum(Rng)
For Each Cell In Rng
If Cell.Value >= 8 Then
Promoters = WorksheetFunction.Count(Cell.Value > 8)
Promoters = Promoters + 1
ElseIf Cell.Value <= 6 Then
Detractors = WorksheetFunction.Count(Cell.Value < 6)
Detractors = Detractors + 1
End If
Next Cell
PourcentagePromoters = (Promoters * 100) / Total
PourcentageDetractors = (Detractors * 100) / Total
NPS = PourcentagePromoters - PourcentageDetractors
End Function
我希望有人能够帮助我,非常感谢!
答案 0 :(得分:2)
您没有正确命名函数返回值NPSCOMPUT
。但是你也可以简化并避免循环不能按预期工作。
此
Promoters = WorksheetFunction.Count(Cell.Value > 8)
应该是
Promoters = Application.WorksheetFunction.CountIf(Rng, ">=" & 8)
等等。
正如@ScottCraner所指出的那样:你不能让一个UDF改变另一个带有该行的单元格
Range("D1").Value = "Net Promotor Score"
此处列出了其他udf限制:
Description of limitations of custom functions in Excel
取决于是否存在空白行:
Total = Rng.Cells.Count
或Application.WorksheetFunction.CountIf(Rng, ">" & 0)
Public Sub test()
With ActiveSheet
MsgBox NPSCOMPUTE(.Range("A1:A10"))
End With
End Sub
Private Function NPSCOMPUTE(ByVal Rng As Range) As Double
Dim Cell As Range
Dim PourcentagePromoters As Double
Dim PourcentageDetractors As Double
Dim Total As Long
Dim Promoters As Long
Dim Detractors As Long
'Range("D1").Value = "Net Promotor Score"
Detractors = 0
Promoters = 0
Total = Rng.Cells.Count ' or Application.WorksheetFunction.CountIf(Rng, ">" & 0)
Promoters = Application.WorksheetFunction.CountIf(Rng, ">=" & 8)
Detractors = Application.WorksheetFunction.CountIf(Rng, "<=" & 6)
PourcentagePromoters = Promoters / Total
PourcentageDetractors = Detractors / Total
NPSCOMPUTE = 100 * (PourcentagePromoters - PourcentageDetractors)
End Function