+ Reply to Thread
Results 1 to 7 of 7

multiple if statements

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-17-2013
    Location
    australia
    MS-Off Ver
    Excel 2007
    Posts
    247

    multiple if statements

    Hi All,

    i am creating some error checking on a template of mine and i need to have a code that runs a bunch of if statements and exits if it doesnt meet the criteria at any step of the process, and the end result if it passes all criteria is 'msgbox go' but i cant seem to get it right - excel 2007


    Sheets("USERPAGE").Activate
    If IsEmpty(Sheets("USERPAGE").Range("D74").Value) = False Then
    If Sheets("USERPAGE").Range("F74") = vbNullString Then
               MsgBox ("Please check Min Nights - Min Nights mandatory when using FOC")
               Exit Sub
            Else
                 
            
        If IsEmpty(Sheets("USERPAGE").Range("B71").Value) = True Then
        MsgBox ("Please check product code - Mandatory")
        Exit Sub
        Else
            
        If IsEmpty(Sheets("USERPAGE").Range("C71").Value) = True Then
        MsgBox ("Please check City Code - Mandatory")
        Exit Sub
        Else
    
        
         If IsEmpty(Sheets("USERPAGE").Range("J71").Value) = True Then
         MsgBox ("Please check child ages - Mandatory")
         Exit Sub
         Else
    
         If IsEmpty(Sheets("USERPAGE").Range("L71").Value) = True Then
         MsgBox ("Please check infant age - Mandatory")
         Exit Sub
         Else
            
        If IsEmpty(Sheets("USERPAGE").Range("D75").Value) = False Then
         If Sheets("USERPAGE").Range("F75") = vbNullString Then
         MsgBox ("Fraction required when Part-day Checkout used")
         Exit Sub
         Else
         MsgBox ("go")
        End If

  2. #2
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: multiple if statements

    Hi 13lack13lade, that's a problem due to poor indentations.


    How your chunk of code looks like when properly indented:
    Sheets("USERPAGE").Activate
    If IsEmpty(Sheets("USERPAGE").Range("D74").Value) = False Then
        If Sheets("USERPAGE").Range("F74") = vbNullString Then
            MsgBox ("Please check Min Nights - Min Nights mandatory when using FOC")
            Exit Sub
        Else
        
            If IsEmpty(Sheets("USERPAGE").Range("B71").Value) = True Then
                MsgBox ("Please check product code - Mandatory")
                Exit Sub
            Else
                
                If IsEmpty(Sheets("USERPAGE").Range("C71").Value) = True Then
                    MsgBox ("Please check City Code - Mandatory")
                    Exit Sub
                Else
                
                    If IsEmpty(Sheets("USERPAGE").Range("J71").Value) = True Then
                        MsgBox ("Please check child ages - Mandatory")
                        Exit Sub
                    Else
                    
                        If IsEmpty(Sheets("USERPAGE").Range("L71").Value) = True Then
                            MsgBox ("Please check infant age - Mandatory")
                            Exit Sub
                        Else
                        
                            If IsEmpty(Sheets("USERPAGE").Range("D75").Value) = False Then
                                If Sheets("USERPAGE").Range("F75") = vbNullString Then
                                    MsgBox ("Fraction required when Part-day Checkout used")
                                    Exit Sub
                                Else
                                    MsgBox ("go")
                                End If
    It becomes very visible that you are missing many End If's. I didn't check through your If statements though, I assume they are working as expected.
    多么想要告诉你 我好喜欢你

  3. #3
    Forum Contributor
    Join Date
    06-17-2013
    Location
    australia
    MS-Off Ver
    Excel 2007
    Posts
    247

    Re: multiple if statements

    Its fine ive figured it out, i separated them into different subs, then created a new sub to call them all, works fine

    Thanks for your help

  4. #4
    Forum Contributor
    Join Date
    06-17-2013
    Location
    australia
    MS-Off Ver
    Excel 2007
    Posts
    247

    Re: multiple if statements

    where do the endif's go because ive tried seperating them all into seperate if statements and also putting all the end ifs at the end and none of which seem to work.

    if i separate them out then it runs but doesnt do anything..

  5. #5
    Forum Contributor lancer102rus's Avatar
    Join Date
    04-02-2014
    Location
    Ufa
    MS-Off Ver
    Excel 2010, 2013
    Posts
    252

    Re: multiple if statements

    try this
    Sheets("USERPAGE").Activate
    If Not IsEmpty(Sheets("USERPAGE").Range("D74").Value) Then
       If Sheets("USERPAGE").Range("F74") = vbNullString Then
            MsgBox "Please check Min Nights - Min Nights mandatory when using FOC"
            Exit Sub
        ElseIf IsEmpty(Sheets("USERPAGE").Range("B71").Value) Then
            MsgBox "Please check product code - Mandatory"
            Exit Sub
        ElseIf IsEmpty(Sheets("USERPAGE").Range("C71").Value) Then
            MsgBox "Please check City Code - Mandatory"
            Exit Sub
        ElseIf IsEmpty(Sheets("USERPAGE").Range("J71").Value) Then
            MsgBox "Please check child ages - Mandatory"
            Exit Sub
        ElseIf IsEmpty(Sheets("USERPAGE").Range("L71").Value) Then
            MsgBox "Please check infant age - Mandatory"
            Exit Sub
        End If
    End If
    
    If Not IsEmpty(Sheets("USERPAGE").Range("D75").Value) Then
        If Sheets("USERPAGE").Range("F75") = vbNullString Then
            MsgBox "Fraction required when Part-day Checkout used"
            Exit Sub
        End If
    End If
    
    MsgBox "go"

  6. #6
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: multiple if statements

    I personally would avoid heavily nested If-Else statements, so I would do it this way, separating each condition:
    Sheets("USERPAGE").Activate
    If IsEmpty(Sheets("USERPAGE").Range("D74").Value) = False Then 'I assume D74 indicates FOC
        If Sheets("USERPAGE").Range("F74") = vbNullString Then
            MsgBox ("Please check Min Nights - Min Nights mandatory when using FOC")
            Exit Sub
        End If
    End If
    
    If IsEmpty(Sheets("USERPAGE").Range("B71").Value) = True Then
        MsgBox ("Please check product code - Mandatory")
        Exit Sub
    End If
    
    If IsEmpty(Sheets("USERPAGE").Range("C71").Value) = True Then
        MsgBox ("Please check City Code - Mandatory")
        Exit Sub
    End If
    
    If IsEmpty(Sheets("USERPAGE").Range("J71").Value) = True Then
        MsgBox ("Please check child ages - Mandatory")
        Exit Sub
    End If
    
    If IsEmpty(Sheets("USERPAGE").Range("L71").Value) = True Then
        MsgBox ("Please check infant age - Mandatory")
        Exit Sub
    End If
    
    If IsEmpty(Sheets("USERPAGE").Range("D75").Value) = False Then 'I assume D75 indicates Part-day Checkout
        If Sheets("USERPAGE").Range("F75") = vbNullString Then
            MsgBox ("Fraction required when Part-day Checkout used")
            Exit Sub
        End If
    End If
    
    MsgBox ("go")

  7. #7
    Forum Contributor
    Join Date
    06-17-2013
    Location
    australia
    MS-Off Ver
    Excel 2007
    Posts
    247

    Re: multiple if statements

    Thanks lancer, mine just ran the statements but didnt actually stop the if statements, your code is exactly what i was looking for!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Nested if statements containing multiple and statements
    By John M. in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 06-22-2014, 11:48 PM
  2. Formula with multiple IF statements and IF AND statements
    By lottidotti in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 11-14-2013, 08:03 AM
  3. Replies: 3
    Last Post: 07-25-2013, 08:25 AM
  4. Multiple nested IF statements and AND statements
    By TonyGetz in forum Excel General
    Replies: 2
    Last Post: 12-14-2010, 03:07 AM
  5. Replies: 12
    Last Post: 05-15-2009, 08:38 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1