excelvba

Excel VBA using IF & OR not activating properly


Little head scratcher I've got I have the below code to clear out entries from a table that don't contain two possible variables, however the 'clear contents' is activating with each row in the table. Is there something wrong with my syntax?

'   Loop through range
    For M2 = 2 To myLastRow2
        If Worksheets("CRE ATW").Cells(M2, "E").Value <> "Signalling" Or _
           Worksheets("CRE ATW").Cells(M2, "E").Value <> "Telecoms" Then _
                 Worksheets("CRE ATW").Cells(M2, "A").ClearContents
    Next M2 

I've tried a few variations on the code such as the below be neither work correctly

    For M2 = 2 To myLastRow2
        If Worksheets("CRE ATW").Cells(M2, "E").Value <> "Signalling" Or "Telecoms" Then _
                Worksheets("CRE ATW").Cells(M2, "A").ClearContents
    Next M2

Solution

  • The syntax is fine, but the effect is probably not what you intended: A<>x OR A<>y is always true, since it can't be both. You probably mean A<>x AND A<>y.