将用户窗体文本框值传递给函数时出现运行时错误 13 类型不匹配
Runtime Error 13 Type Mismatch when passing a userform textbox value to a function
我有一个带有文本框的用户表单,其中一些应该包含整数。我在互联网上的某个地方(我想是在这个网站上)找到了这个 IsInteger 函数,我将它存储在一个模块中,只是为了函数:
Function IsInteger()
If IsNumeric(testsubject) Then
If testsubject - Int(testsubject) <> 0 Then
integerYes = True
Else: integerYes = False
End If
End If
End Function
以下代码块与用户窗体文本框之一相关:
Private Sub IB_LoanTermYears_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'Data validation
If Me.IB_LoanTermYears.Value = "" Then
ElseIf IsNumeric(Me.IB_LoanTermYears.Value) = False Then
blahAnswer = MsgBox("Please enter a valid number.", , "Invalid Entry")
Me.IB_LoanTermYears.SetFocus
ElseIf IsInteger(Me.IB_LoanTermYears.Value) = False Then
blahAnswer = MsgBox("Please enter a whole number.", , "Invalid Entry")
Me.IB_LoanTermYears.SetFocus
ElseIf Me.IB_LoanTermYears.Value < 0 Then
blahanswer MsgBox("Please enter a positive number.", , "Invalid Entry")
Me.IB_LoanTermYears.SetFocus
End If
End Sub
当我退出文本框时,出现运行时错误 13,其中 ElseIf IsInteger(Me.IB_LoanTermYears.Value) = False Then
突出显示。是什么赋予了?我看到一个非常相似的 post 结论是其他空文本框搞砸了,但我看不出这会如何影响这个文本框,因为它们没有以任何方式链接,除了它们在同一个用户表单。
谢谢!
确保函数接受输入且输出为布尔值。
由于布尔值的默认值是 FALSE 我们只需要更改为 True
Function IsInteger(testsubject As Variant) As Boolean
If IsNumeric(testsubject) Then
IsInteger = (testsubject - CInt(testsubject) = 0)
End If
End Function
出现错误是因为函数不接受参数。这是使用 Integer 函数的更好方法,它实际上会检查解析的变体是否可以无损地转换为 Long
。例如:Clng(43.4)
将 return 43
,因此出现损失:
Function ParseToLong(testSubject As Variant) As Boolean
If IsNumeric(testSubject) Then
If testSubject - CLng(testSubject) = 0 Then
ParseToInteger = True
Exit Function
End If
End If
ParseToInteger = False
End Function
请记住,ParseToLong("32")
会 return True
,只要字符串“32”可以转换为数字 32
.
我有一个带有文本框的用户表单,其中一些应该包含整数。我在互联网上的某个地方(我想是在这个网站上)找到了这个 IsInteger 函数,我将它存储在一个模块中,只是为了函数:
Function IsInteger()
If IsNumeric(testsubject) Then
If testsubject - Int(testsubject) <> 0 Then
integerYes = True
Else: integerYes = False
End If
End If
End Function
以下代码块与用户窗体文本框之一相关:
Private Sub IB_LoanTermYears_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'Data validation
If Me.IB_LoanTermYears.Value = "" Then
ElseIf IsNumeric(Me.IB_LoanTermYears.Value) = False Then
blahAnswer = MsgBox("Please enter a valid number.", , "Invalid Entry")
Me.IB_LoanTermYears.SetFocus
ElseIf IsInteger(Me.IB_LoanTermYears.Value) = False Then
blahAnswer = MsgBox("Please enter a whole number.", , "Invalid Entry")
Me.IB_LoanTermYears.SetFocus
ElseIf Me.IB_LoanTermYears.Value < 0 Then
blahanswer MsgBox("Please enter a positive number.", , "Invalid Entry")
Me.IB_LoanTermYears.SetFocus
End If
End Sub
当我退出文本框时,出现运行时错误 13,其中 ElseIf IsInteger(Me.IB_LoanTermYears.Value) = False Then
突出显示。是什么赋予了?我看到一个非常相似的 post 结论是其他空文本框搞砸了,但我看不出这会如何影响这个文本框,因为它们没有以任何方式链接,除了它们在同一个用户表单。
谢谢!
确保函数接受输入且输出为布尔值。
由于布尔值的默认值是 FALSE 我们只需要更改为 True
Function IsInteger(testsubject As Variant) As Boolean
If IsNumeric(testsubject) Then
IsInteger = (testsubject - CInt(testsubject) = 0)
End If
End Function
出现错误是因为函数不接受参数。这是使用 Integer 函数的更好方法,它实际上会检查解析的变体是否可以无损地转换为 Long
。例如:Clng(43.4)
将 return 43
,因此出现损失:
Function ParseToLong(testSubject As Variant) As Boolean
If IsNumeric(testSubject) Then
If testSubject - CLng(testSubject) = 0 Then
ParseToInteger = True
Exit Function
End If
End If
ParseToInteger = False
End Function
请记住,ParseToLong("32")
会 return True
,只要字符串“32”可以转换为数字 32
.