Excel VBA
Excel根据条件作判定工具
2017-03-23 17:04:34

    在我们工作中有很多麻烦的数据需要我们来判定结果,如下图所示,共有5项测试,每个样品不用每一项都测试,未测试的项目填“/”,规格为“TBD”的项目只提供参考,不做判定依据,部分测试项目的结果为“OK”或“NG”,如果要用函数来解决,是一件麻烦的事,今天教大家VBA解决这个问题。

效果演示如下

首先,点击录制宏,将保存位置改为个人宏工作薄。

代码解析如下图

代码如下:

Dim a As Long, b As Long, c As Byte, d As Byte

Sub panding()

    For a = Selection.Row To ActiveSheet.UsedRange.Rows.Count

        d = 0

        For b = 6 To ActiveSheet.UsedRange.Columns.Count

            If Cells(a, b) = "NG" Then

                d = d + 1

            End If

            If Application.WorksheetFunction.IsNumber(Cells(2, b)) And Application.WorksheetFunction.IsNumber(Cells(a, b)) Then

               If Cells(a, b) > Cells(2, b) Then

                    d = d + 1

                End If

            End If

            If Application.WorksheetFunction.IsNumber(Cells(3, b)) And Application.WorksheetFunction.IsNumber(Cells(a, b)) Then

                If Cells(a, b) < Cells(3, b) Then

                d = d + 1

            End If

        End If

    Next

    If d > 0 Then

        Cells(a, 5) = "NG"

    Else

        Cells(a, 5) = "OK"

    End If

    Next

End Sub