PDA

View Full Version : [SOLVED:] Conditional Statements & Nested If/Then in VBA



jjjhs12
02-09-2018, 06:51 AM
I am trying to write a function or sub which passes in 3 variables from the worksheet and returns a number 0-3 based on conditional statements (nested If/then). However, the func returns ‘0’ for all cases. Using breaks in the module, I can see this is because, while the variables have been passed in (UpIso, Con, DIso), the function (isolation) =’empty’

My code is below. Any help is appreciated. Thanks

Function Isolation(UpIso, Con, DIso) As Variant

Dim Y As Variant
Dim N As Variant
Dim NA As Variant
Dim FD As Variant
Dim BP As Variant
Dim Station as Variant

'Free discharge
If (UpIso = Y And Con = FD And (DIso = Y Or DIso = NA)) Then
Isolation = 0
ElseIf (UpIso = N And Con = FD And (DIso = Y Or DIso = NA)) Then
Isolation = 3

'Back Pressure
ElseIf (UpIso = Y And Con = BP And DIso = Y) Then
Isolation = 0

ElseIf (UpIso = Y And Con = BP And DIso = N) Then
Isolation = 2

ElseIf (UpIso = Station And Con = BP And DIso = Y) Then
Isolation = 1

ElseIf (UpIso = Station And Con = BP And DIso = N) Then
Isolation = 2

ElseIf (UpIso = N And Con = BP And DIso = Y) Then
Isolation = 2

ElseIf (UpIso = N And Con = BP And DIso = N) Then
Isolation = 3

End If

End Function

jjjhs12
02-09-2018, 07:57 AM
Solved. Quotations needed around variables.


Function Isolation(UpIso, Con, DIso) As Variant

Dim Y As Variant
Dim N As Variant
Dim NA As Variant
Dim FD As Variant
Dim BP As Variant
Dim Station as Variant

'Free discharge
If (UpIso = Y And Con = FD And (DIso = Y Or DIso = NA)) Then
Isolation = 0
ElseIf (UpIso = N And Con = FD And (DIso = Y Or DIso = NA)) Then
Isolation = 3

'Back Pressure
ElseIf (UpIso = Y And Con = BP And DIso = Y) Then
Isolation = 0

ElseIf (UpIso = Y And Con = BP And DIso = N) Then
Isolation = 2

ElseIf (UpIso = Station And Con = BP And DIso = Y) Then
Isolation = 1

ElseIf (UpIso = Station And Con = BP And DIso = N) Then
Isolation = 2

ElseIf (UpIso = N And Con = BP And DIso = Y) Then
Isolation = 2

ElseIf (UpIso = N And Con = BP And DIso = N) Then
Isolation = 3

End If

End Function