【问题标题】:Function Subroutine : Boolean函数子程序:布尔值
【发布时间】:2018-07-20 18:41:31
【问题描述】:

我在调用布尔函数的过程中使用了 if 语句;但是,当我运行程序时,它总是输出消息,就好像函数是真的一样。

Public Function INR() As Boolean
    If Avg < 1.5 & SD < 0.5 Then
        INR = True
    Else
        INR = False
    End If
End Function

Public Sub PatientINR()
    Range("B2", Range("B1").End(xlDown)).Name = "Name"
    Dim cell As Range, reply As String, i As Long, x As Variant, Avg As Long, SD As Long
    reply = InputBox("Enter patient's name", "INR", "Madelyn Carberry")
        i = 1
    x = Range("Name").Cells(i)
    Do Until x = reply
        i = i + 1
        x = Range("Name").Cells(i)
    Loop
    Avg = Range("Name").Cells(i, 17)
    SD = Range("Name").Cells(i, 18)
    Call INR
    If INR = True Then
        MsgBox reply & "'s INR record is satisfactory for the procedure"
    End If
    If INR = False Then
        MsgBox reply & "'s INR record is not satisfactory for the procedure"
    End If

End Sub

【问题讨论】:

  • 函数中AvgSD是如何设置的。用户函数应该只处理局部变量和参数。

标签: vba excel function if-statement boolean


【解决方案1】:

您需要将数据传递给函数。将函数的第一行改为:

Public Function INR(Avg As Double, SD As Double) As Boolean

然后当你调用函数时传递变量:

If INR(Avg,SD) Then

按原样,这些值没有被传递,因此在测试时为 0。这将返回 true。

正如您在上面看到的,您不需要 = True,它是一个布尔值,因此已经是 True 或 False。

也不要测试 False 只需使用 Else:

If INR(Avg,SD) Then
    MsgBox reply & "'s INR record is satisfactory for the procedure"
Else
    MsgBox reply & "'s INR record is not satisfactory for the procedure"
End If

&amp; 也用于串联,使用单词And

If Avg < 1.5 And SD < 0.5 Then

函数在if中被调用,不需要Call INR,就像其他函数一样使用。

另外,命名运行它的工作表也是一个好习惯。我们可以通过 With 块来做到这一点:

With Worksheets("Sheet1") 'Change to your sheet
    'Do your code
End With

我们可以使用 Range 变量来代替创建命名范围:

Set rng = .Range("B:B")

让我们加快速度并删除循环。我们可以使用 Match 来查找包含以下内容的行:

i = Application.WorksheetFunction.Match(reply, rng, 0)

如果找到该名称,它将返回找到该名称的行。如果不是,它将返回错误。如果出错,我选择跳过,因此i 将保持为 0,我们可以在继续之前对其进行测试。

INR 的默认值为 False,因此我们不需要设置它并节省一些输入:

INR = (Avg < 1.5 And SD < 0.5)

应该够了

所以:

Public Function INR(Avg As Double, SD As Double) As Boolean
    INR = (Avg < 1.5 And SD < 0.5)
End Function

Public Sub PatientINR()

    Dim cell As Range, reply As String, i As Long
    Dim Avg As Double, SD As Double
    Dim rng As Range
    reply = InputBox("Enter patient's name", "INR", "Madelyn Carberry")
    i = 0
    With Worksheets("Sheet1") 'Change to your sheet
        Set rng = .Range("B:B")
        On Error Resume Next
            i = Application.WorksheetFunction.Match(reply, rng, 0)
        On Error GoTo 0
        If i <> 0 Then
            Avg = .Cells(i, 17)
            SD = .Cells(i, 18)
            If INR(Avg, SD) Then
                MsgBox reply & "'s INR record is satisfactory for the procedure"
            Else
                MsgBox reply & "'s INR record is not satisfactory for the procedure"
            End If
        Else
            MsgBox "Name not Found"
        End If
    End With

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-13
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 2021-12-06
    • 2013-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多