If logic where either of two conditions make it true

时光毁灭记忆、已成空白 提交于 2021-01-29 07:50:27

问题


I am trying to delete a row if cells in column V is not equal to F or V.

This code deletes everything.

For i = RowCount To 1 Step -1
    If Range("V" & i).Value <> "F" Or Range("V" & i).Value <> "V" Then Rows(i).Delete
Next i

回答1:


Your logic seems incorrect. By changing the or to an and, you will then be evaluating the proper boolean arithmetic.

Right now, you are checking not "v" and not "f" which is always false. No matter the input, this will always have answer be true.




回答2:


Answer to Else Not working in "If Then Else" in VBA EXCEL

You need something similar

Sub SpecificFlatArrears03()
    
    Dim Flat As Variant
    
    Do
        Flat = InputBox("Pl Enter a Flat Number as 3 Digit without Prefix")
        'at this point you should check that Flat meets the required requirements
        Range("h3").Value = Flat
        If (Flat > 100 And Flat < 165) Or (Flat > 200 And Flat < 264) Or (Flat > 300 And Flat < 364) Or Flat = 403 Or Flat = 404 Or Flat = 462 Or Flat = 463 Then
            Range("h4") = WorksheetFunction.Match(Flat, Range("'2020-2021'!d11:d206"), 0)
            Exit Do
        End If
    Loop Until False
    
End Sub


来源:https://stackoverflow.com/questions/64642028/else-not-working-in-if-then-else-in-vba-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!