excel - Excel VBA 自定义分数公式
问题描述
美好的一天,我正在尝试在 excel 中创建一个函数,该函数根据其他 4 个数字对数字进行评分,我正在为预算制作记分卡,因此我正在尝试完成以下操作:
现在我有 4 列,(K 到 N)“隐藏”,其中包含值 157、143、128、114。我想创建一个公式,将 A5 中的值与其他四列进行比较,然后将结果放在 J 列中。例如,我会将以下公式放在 J5 中
单元格 J5= 分数(A5、K5、L5、M5、N5)
为了获得评级,我创建了以下 VBA 代码:
Public Function Score(A As Integer, S1 As Integer, S2 As Integer, S3 As Integer, S4 As Integer) As String
If A < S1 Then
Score = "1"
ElseIf S1 + 1 <= A < S2 Then
Score = "2"
ElseIf S2 + 1 <= A < S3 Then
Score = "3"
ElseIf S3 + 1 <= A < S4 Then
Score = "4"
ElseIf S4 + 1 <= A Then
Score = "5"
End If
End Function
有人可以告诉我哪里出错了吗?当我使用公式时出现#NUM
错误
解决方案
如果这是您的第一次尝试,请尝试一下。您必须指定函数的返回值 -As String
作为第一行的结尾。
Public Function Score() As String
Dim iRow As Integer
iRow = Application.Caller.Row
A = Range("$A$5")
S1 = Range("$K" & iRow)
S2 = Range("$L" & iRow)
S3 = Range("$M" & iRow)
S4 = Range("$N" & iRow)
If A < S1 Then
Score = "1"
ElseIf S1 + 1 <= A < S2 Then
Score = "2"
ElseIf S2 + 1 <= A < S3 Then
Score = "3"
ElseIf S3 + 1 <= A < S4 Then
Score = "4"
ElseIf S4 + 1 <= A Then
Score = "5"
End If
End Function
如果您想自己为功能选择五个单元格,例如=score(A5,F10, G10, H10, E10)
等
Public Function Score(A As Integer, S1 As Integer, S2 As Integer, S3 As Integer, S4 As Integer) As String
If A < S1 Then
Score = "1"
ElseIf S1 + 1 <= A < S2 Then
Score = "2"
ElseIf S2 + 1 <= A < S3 Then
Score = "3"
ElseIf S3 + 1 <= A < S4 Then
Score = "4"
ElseIf S4 + 1 <= A Then
Score = "5"
End If
End Function
推荐阅读
- matlab - 用于独立可执行文件的 Matlab 编码器
- c++ - cout 在 for 循环中切断字符串的开头
- javascript - 带有href的SVG animateTransform
- reactjs - 如何在带有装饰器的 react-native 0.56 (Babel 7) 中使用 mobx
- c# - 给定国家 - 如何以编程方式从 CultureInfo 获取每个显示名称?
- qr-code - 二维码触发事件的工作原理
- r - plotly::ggplotly 正在破坏某些 facet-wrap 数据
- django - 当我只尝试编辑时,Django Admin save_model() 创建一个新对象
- java - 仅当子节点存在时如何检索子节点的文本
- html - 居中位置:桌面和移动设备中相对容器中的绝对 div