Results 1 to 8 of 8

Thread: IF statements with AND or OR

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    4

    Resolved IF statements with AND or OR

    I know how to do IF statements in visual basic, but how do I incorporate AND and OR statements? ie, a simple if statement is:


    if UserForm.CheckBox1 = True Then

    statement

    End If

    But I want one that says:

    if UserForm.CheckBox1 = True or if UserForm1.CheckBox 2 = True Then

    statement

    End If

    and the same again, but using AND not or

    Thanks for any help given.
    Last edited by red6000; May 9th, 2005 at 05:48 PM.

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: IF statements with AND or OR

    Like this:

    if UserForm.CheckBox1 = True or if UserForm1.CheckBox 2 = True Then

    statement

    End If
    First OR then AND

    VB Code:
    1. if UserForm.CheckBox1 = True or _
    2.    UserForm1.CheckBox2 = True Then
    3. ' statement
    4. End If

    VB Code:
    1. if UserForm.CheckBox1 = True and _
    2.    UserForm1.CheckBox2 = True Then
    3. ' statement
    4. End If

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: IF statements with AND or OR

    Welcome to the forums red6000.

    As a note, your "And" conditions need to come first before any "Or" conditions.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    4

    Re: IF statements with AND or OR

    Thanks for the replies...

    so lets say I want to process the statement if:

    UserForm1.Checkbox1.Value = True AND UserForm1.Checkbox2.Value = True

    OR if just UserForm1.Checkbox3.Value = True

    as opposed to if:

    UserForm1.Checkbox1.Value = True

    AND if UserForm1.Checkbox2.Value = True OR if UserForm1.Checkbox3.Value = True

    then how would it look? Could I have examples of both please if possible.

    Hope that makes sense

    Thanks for the help!!

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: IF statements with AND or OR

    I think I understand your request...
    VB Code:
    1. If ((UserForm1.Checkbox1.Value = True) AND (UserForm1.Checkbox2.Value = True)) OR (UserForm1.Checkbox3.Value = True) Then
    2.     'Either both the first two are checked OR just the third one
    3. End If
    4.  
    5. If (UserForm1.Checkbox1.Value = True) AND (UserForm1.Checkbox2.Value = True) AND (UserForm1.Checkbox3.Value = True) Then
    6.     'All three are checked
    7. End If
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    4

    Re: IF statements with AND or OR

    The 2nd one was for the statement to be process if the 1st box is ticked and either of the other 2, so would it be:


    VB Code:
    1. If (UserForm1.Checkbox1.Value = True) AND ((UserForm1.Checkbox2.Value = True) OR (UserForm1.Checkbox3.Value = True)) Then

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: IF statements with AND or OR

    Correct. I wasnt too sure how you needed the grouping but you picked this up quite nicely.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    4

    Re: IF statements with AND or OR

    Thanks for the help. That's going to make my code so much easier instead of using loads of nested IFs to get the same effect!

    Thanks again.

Posting Permissions

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



Click Here to Expand Forum to Full Width