+ Reply to Thread
Results 1 to 4 of 4

If/And/OR

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-16-2004
    MS-Off Ver
    2016
    Posts
    175

    If/And/OR

    How do I write the code below so it works? Currently it deletes all the "B's" in the column when it should only delete 1 or 2 "B"s" that fit the condition. If I write the code one condition at a time without the "or's" in separate macros that call each other it works but that would be a lot of writing.



    finalrow = Cells(Rows.Count, 4).End(xlUp).Row
    For x = 3 To finalrow
    If Cells(x, 11).Value = "B" And Cells(x, 20).Value = "M" Or Cells(x, 20).Value = "Mo" Or Cells(x, 20).Value = "S" And Cells(x, 16).Value <> "M" Or Cells(x, 16).Value <> "Mo" Or Cells(x, 16).Value <> "S" Then Cells(x, 11).Value = ""
    Next

  2. #2
    Forum Contributor
    Join Date
    12-30-2021
    Location
    Mount Gambier, Australia
    MS-Off Ver
    Microsoft Office Home and Student 2019
    Posts
    113

    Re: If/And/OR

    Can you upload a file with some data

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: If/And/OR

    Group the conditions for the three tested cells in a given row.

    Group conditions with parenthesis like this...
    If Cells(x, 11).Value = "B" And (Cells(x, 20).Value = "M" Or Cells(x, 20).Value = "Mo" Or Cells(x, 20).Value = "S") And (Cells(x, 16).Value <> "M" And Cells(x, 16).Value <> "Mo" And Cells(x, 16).Value <> "S") Then Cells(x, 11).Value = ""
    Or group conditions with nested IFs like this...
    For x = 3 To finalrow
    If Cells(x, 11).Value = "B" Then
        If Cells(x, 20).Value = "M" Or Cells(x, 20).Value = "Mo" Or Cells(x, 20).Value = "S" Then
            If Cells(x, 16).Value <> "M" And Cells(x, 16).Value <> "Mo" And Cells(x, 16).Value <> "S" Then
                Cells(x, 11).Value = ""
            End If
        End If
    End If
    Next
    Last edited by AlphaFrog; 01-20-2022 at 01:07 AM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  4. #4
    Forum Contributor
    Join Date
    03-16-2004
    MS-Off Ver
    2016
    Posts
    175

    Re: If/And/OR

    Thanks Alpha! Good tip on the grouping.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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