如何轻松地将值与范围内的其他值进行比较

时间:2018-11-11 05:51:42

标签: excel vba

有人可以帮忙,如何以某种更聪明(而且肯定更短)的方式重写以下条件?

应该说:如果一个特定单元格中的值不为空,并且同时不同于X32到X47单元格中的任何值,则→做某事

非常感谢!

For i = 4 To 69
    If Range(SO_COLUMN & i) <> "" _
        And Range(SO_COLUMN & i) <> Range("X32") And Range(SO_COLUMN & i) <> Range("X33") And Range(SO_COLUMN & i) <> Range("X34") And Range(SO_COLUMN & i) <> Range("X35") _
        And Range(SO_COLUMN & i) <> Range("X36") And Range(SO_COLUMN & i) <> Range("X37") And Range(SO_COLUMN & i) <> Range("X38") And Range(SO_COLUMN & i) <> Range("X39") _
        And Range(SO_COLUMN & i) <> Range("X40") And Range(SO_COLUMN & i) <> Range("X41") And Range(SO_COLUMN & i) <> Range("X42") And Range(SO_COLUMN & i) <> Range("X43") _
        And Range(SO_COLUMN & i) <> Range("X44") And Range(SO_COLUMN & i) <> Range("X45") And Range(SO_COLUMN & i) <> Range("X46") And Range(SO_COLUMN & i) <> Range("X47") Then

            "DO SOMETHING"
    End If
Next i

1 个答案:

答案 0 :(得分:2)

也许像这样:

With ThisWorkbook.Worksheets("yourSheetName")
    For i = 4 To 69
        If Not IsEmpty(.Range(SO_COLUMN & i)) And IsError(Application.Match(.Range(SO_COLUMN & i), .Range("X32:X47"), 0)) Then
            'do something
        End If
    Next
End With