Home ElseIf statement skipped when it shouldn't be with "or"
Reply: 2

ElseIf statement skipped when it shouldn't be with "or"

James Fenwick
1#
James Fenwick Published in 2018-02-14 15:39:26Z

I'm building a little program to help out with some data imports and for our techs when out on sites.

The bit I'm struggling with is the bp check, see code below:

Private Sub bphg_afterupdate()

'blood pressure values
'below 100/60 - low
'120/70 - normal
'140/90 - high, gp review
'180/100 - high, cut off for fitness for driving
'200/100 - high, cut off for driving/spiro
'230/120 - urgent review required


    If bpmm <= 100 Or bphg <= 60 Then
        bpcomment.Value = "LOW! - Seek Advice"
    ElseIf bpmm < 140 Or bphg < 90 Then
        bpcomment.Value = "Normal BP"
    ElseIf bpmm < 180 Or bphg < 100 Then
        bpcomment.Value = "High! - GP Review"
    ElseIf bpmm < 200 Then
        bpcomment.Value = "High! - Temp restriction to driving MPE/FLT"
    ElseIf bpmm < 230 Or bphg < 120 Then
        bpcomment.Value = "High! - To high for Spiro & Temp Driving Resitricion MPE/FLT"
    Else
        bpcomment.Value = "URGENT! - Review required"
    End If

End Sub

What it's doing is finding the first value that fits in either the values specified and then stopping. It should be continuing to check other criteria.

So basically with blood pressure, out of the 2 figures your doctor gives you, either can determine if your bp is ok or not. So when we enter a bp into the form say 200/80 (you would probably never get this but I'm being through), it would find that the first figure is high and the second is normal. My script however is finding the second figure being normal first without checking the first figure, so it just displays "normal" when in fact it's "high".

Vityata
2#
Vityata Reply to 2018-02-14 16:19:15Z

Select Case would be a better way to deal with the blood pressure issues:

Option Explicit

Public Sub TestMe()

    Dim bpmm        As Long
    Dim bphg        As Long

    bpmm = 100   'assign these two somehow. E.g.-> bpmm = ActiveSheet.Range("A1")
    bphg = 100

    Select Case True
        Case bpmm <= 100 Or bphg <= 60
            Debug.Print "LOW! - Seek Advice"
        Case bpmm < 140 Or bphg < 90
            Debug.Print "Normal BP"
        Case bpmm < 180 Or bphg < 100
            Debug.Print "High!"
        Case Else
            Debug.Print "URGENT! - Review required"
    End Select

End Sub

Instead of Debug.Print, you may put your business logic there. Just make sure that you order the conditions correctly - if the first condition is evaluated to TRUE, the check does not go further.

There is a tiny performance advantage of Select Case - Which way is faster? If elseif or select case

Edit: If your logic is, that all the criteria should be checked separately and independently, then take a look at this:

Public Sub TestMe()

    Dim bpmm        As Long
    Dim bphg        As Long

    bpmm = 100   'assign these two somehow. E.g.-> bpmm = ActiveSheet.Range("A1")
    bphg = 100

    If bpmm <= 100 Or bphg <= 60 Then
        Debug.Print "LOW! - Seek Advice"
    End If
    If bpmm < 140 Or bphg < 90 Then
        Debug.Print "Normal BP"
    End If
    If bpmm < 180 Or bphg < 100 Then
        Debug.Print "High!"
    End If

End Sub
Racil Hilan
3#
Racil Hilan Reply to 2018-02-14 16:24:05Z

From your description, it looks like you want to return the result of the highest one of the two criteria. In this case, you will need to reverse the order of the checks:

If bpmm >= 230 Or bphg >= 120 Then
    bpcomment.Value = "URGENT! - Review required"
ElseIf bpmm >= 200 Then
    bpcomment.Value = "High! - To high for Spiro & Temp Driving Resitricion MPE/FLT"
ElseIf bpmm >= 180 Or bphg >= 100 Then
    bpcomment.Value = "High! - Temp restriction to driving MPE/FLT"
ElseIf bpmm >= 140 Or bphg >= 90 Then
    bpcomment.Value = "High! - GP Review"
ElseIf bpmm >= 120 Or bphg >= 70 Then
    bpcomment.Value = "Normal BP"
Else
    bpcomment.Value = "LOW! - Seek Advice"
End If

However, it's not clear from the instructions (that you have at the top of your function) what to do in the case it is between 100/60 and 120/70. The code above considers it LOW. If you want to consider it NORMAL instead, then change the last ElseIf from:

ElseIf bpmm >= 120 Or bphg >= 70 Then

to:

ElseIf bpmm >= 100 Or bphg >= 60 Then
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.297704 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO