Consulting

Results 1 to 2 of 2

Thread: Conditional Statements & Nested If/Then in VBA

  1. #1
    VBAX Newbie
    Joined
    Feb 2018
    Posts
    2
    Location

    Conditional Statements & Nested If/Then in VBA

    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

  2. #2
    VBAX Newbie
    Joined
    Feb 2018
    Posts
    2
    Location
    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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •