在函数中使用 IsNumeric
Using IsNumeric in a function
我写了一个函数,它根据一个输入单元格生成一个 "score" (1,0,-1),它应该包含一个数字。但是,有时输入字段可能不是数字,然后函数应该 return 输出“0”。
Function ScoreRoE(RoE_Field As Range, goodval As Range, badval As Range)
Dim RoE As Double, result As Double
RoE = RoE_Field.Value
If IsNumeric(RoE_Field.Value) = False Then
result = "0"
Else:
If RoE >= goodval.Value Then
result = "1"
ElseIf RoE <= badval.Value Then
result = "-1"
Else:
result = "0"
End If
End If
ScoreRoE = result
End Function
当输入单元格为数字时,该函数可以正常工作。但是,如果不是,则只是 return 错误“#VALUE!”
提前致谢!
将 RoE 声明为变体:
Function ScoreRoE(RoE_Field As Range, goodval As Range, badval As Range)
Dim RoE As Variant, result As Double
RoE = RoE_Field.Value
If Not IsNumeric(RoE) Then
result = 0
Else
If RoE >= goodval.Value Then
result = 1
ElseIf RoE <= badval.Value Then
result = -1
Else
result = 0
End If
End If
ScoreRoE = result
End Function
您不能将文本值分配给双精度值。
我个人认为不需要任何变量:
Function ScoreRoE(RoE_Field As Range, goodval As Range, badval As Range)
If Not IsNumeric(RoE_Field.Value) Then
ScoreRoE = 0
Else
If RoE_Field.Value >= goodval.Value Then
ScoreRoE = 1
ElseIf RoE_Field.Value <= badval.Value Then
ScoreRoE = -1
Else
ScoreRoE = 0
End If
End If
End Function
这是对代码意图的解读:
Function ScoreRoe(roeInput As String, goodVal As String, badVal As String) As String
If Not IsNumeric(roeInput) Or Not IsNumeric(goodVal) Or Not IsNumeric(badVal) Then
ScoreRoe = "0"
Else
Dim goodValNumeric As Double: goodValNumeric = goodVal
Dim badValNumeric As Double: badValNumeric = badVal
Dim roeInputNumeric As Double: roeInputNumeric = roeInput
If roeInputNumeric >= goodValNumeric Then
ScoreRoe = "1"
ElseIf roeInputNumeric <= badValNumeric Then
ScoreRoe = "-1"
Else
ScoreRoe = "0"
End If
End If
End Function
主要思路是将输入取为String
,然后转为Double
进行比较。然后,如果输入不是数字,则返回值为 0
,如果是,我们将通过 If-Else。
一般来说,避免在 VBA If-Else
中使用 :
可能是个好主意,以避免像这样的不必要的问题 -
我写了一个函数,它根据一个输入单元格生成一个 "score" (1,0,-1),它应该包含一个数字。但是,有时输入字段可能不是数字,然后函数应该 return 输出“0”。
Function ScoreRoE(RoE_Field As Range, goodval As Range, badval As Range)
Dim RoE As Double, result As Double
RoE = RoE_Field.Value
If IsNumeric(RoE_Field.Value) = False Then
result = "0"
Else:
If RoE >= goodval.Value Then
result = "1"
ElseIf RoE <= badval.Value Then
result = "-1"
Else:
result = "0"
End If
End If
ScoreRoE = result
End Function
当输入单元格为数字时,该函数可以正常工作。但是,如果不是,则只是 return 错误“#VALUE!”
提前致谢!
将 RoE 声明为变体:
Function ScoreRoE(RoE_Field As Range, goodval As Range, badval As Range)
Dim RoE As Variant, result As Double
RoE = RoE_Field.Value
If Not IsNumeric(RoE) Then
result = 0
Else
If RoE >= goodval.Value Then
result = 1
ElseIf RoE <= badval.Value Then
result = -1
Else
result = 0
End If
End If
ScoreRoE = result
End Function
您不能将文本值分配给双精度值。
我个人认为不需要任何变量:
Function ScoreRoE(RoE_Field As Range, goodval As Range, badval As Range)
If Not IsNumeric(RoE_Field.Value) Then
ScoreRoE = 0
Else
If RoE_Field.Value >= goodval.Value Then
ScoreRoE = 1
ElseIf RoE_Field.Value <= badval.Value Then
ScoreRoE = -1
Else
ScoreRoE = 0
End If
End If
End Function
这是对代码意图的解读:
Function ScoreRoe(roeInput As String, goodVal As String, badVal As String) As String
If Not IsNumeric(roeInput) Or Not IsNumeric(goodVal) Or Not IsNumeric(badVal) Then
ScoreRoe = "0"
Else
Dim goodValNumeric As Double: goodValNumeric = goodVal
Dim badValNumeric As Double: badValNumeric = badVal
Dim roeInputNumeric As Double: roeInputNumeric = roeInput
If roeInputNumeric >= goodValNumeric Then
ScoreRoe = "1"
ElseIf roeInputNumeric <= badValNumeric Then
ScoreRoe = "-1"
Else
ScoreRoe = "0"
End If
End If
End Function
主要思路是将输入取为String
,然后转为Double
进行比较。然后,如果输入不是数字,则返回值为 0
,如果是,我们将通过 If-Else。
一般来说,避免在 VBA If-Else
中使用 :
可能是个好主意,以避免像这样的不必要的问题 -