Excel if语句缩短了吗?

时间:2017-01-24 09:15:31

标签: excel vba excel-vba if-statement

我根据不同单元格中的值将VBA代码写入颜色单元格。是否有可能缩短以下时间?我是VBA脚本的新手,并且希望如果可以缩短它,那么我可以将它应用到更大的文件中。

 Sub RoutingCheck()
   Dim I As Long, r1 As Range, r2 As Range

   For I = 2 To 456
      Set r1 = Range("A" & I)
      Set r2 = Range("B" & I)

    If r1.Value = 94 And r2.Value = " " Then r1.Interior.Color = vbRed
    If r1.Value = 94 And r2.Value = "" Then r1.Interior.Color = vbRed
    If r1.Value = 94 And r2.Value = -99 Then r1.Interior.Color = vbRed
    If r1.Value = 94 And r2.Value = -66 Then r1.Interior.Color = vbRed
    If r1.Value = 94 And r2.Value = -77 Then r1.Interior.Color = vbRed
    If r1.Value = 94 And r2.Value = " " Then r2.Interior.Color = vbRed
    If r1.Value = 94 And r2.Value = "" Then r2.Interior.Color = vbRed
    If r1.Value = 94 And r2.Value = -99 Then r2.Interior.Color = vbRed
    If r1.Value = 94 And r2.Value = -66 Then r2.Interior.Color = vbRed
    If r1.Value = 94 And r2.Value = -77 Then r2.Interior.Color = vbRed
    If r1.Value = 94 And r2.Value <> "" And r2.Value <> -99 And r2.Value <> -66 And r2.Value <> -77 And r2.Value <> " " Then r1.Interior.Color = vbGreen
    If r1.Value = 94 And r2.Value <> "" And r2.Value <> -99 And r2.Value <> -66 And r2.Value <> -77 And r2.Value <> " " Then r2.Interior.Color = vbGreen

    If r1.Value = 1 And r2.Value = "" Then r1.Interior.Color = vbRed
    If r1.Value = 1 And r2.Value = -66 Then r1.Interior.Color = vbRed
    If r1.Value = 1 And r2.Value = 0 Then r1.Interior.Color = vbRed
    If r1.Value = 1 And r2.Value = -77 Then r1.Interior.Color = vbRed
    If r1.Value = 1 And r2.Value = "" Then r2.Interior.Color = vbRed
    If r1.Value = 1 And r2.Value = -66 Then r2.Interior.Color = vbRed
    If r1.Value = 1 And r2.Value = 0 Then r2.Interior.Color = vbRed
    If r1.Value = 1 And r2.Value = -77 Then r2.Interior.Color = vbRed
    If r1.Value = 1 And r2.Value <> "" And r2.Value <> -66 And r2.Value <> -77 And r2.Value <> 0 And r2.Value <> -99 Then r1.Interior.Color = vbRed
    If r1.Value = 1 And r2.Value <> "" And r2.Value <> -66 And r2.Value <> -77 And r2.Value <> 0 And r2.Value <> -99 Then r2.Interior.Color = vbRed
    If r1.Value = 1 And r2.Value = -99 Then r1.Interior.Color = vbGreen
    If r1.Value = 1 And r2.Value = -99 Then r2.Interior.Color = vbGreen

    If r1.Value = 2 And r2.Value = "" Then r1.Interior.Color = vbRed
    If r1.Value = 2 And r2.Value = -66 Then r1.Interior.Color = vbRed
    If r1.Value = 2 And r2.Value = 0 Then r1.Interior.Color = vbRed
    If r1.Value = 2 And r2.Value = -77 Then r1.Interior.Color = vbRed
    If r1.Value = 2 And r2.Value = "" Then r2.Interior.Color = vbRed
    If r1.Value = 2 And r2.Value = -66 Then r2.Interior.Color = vbRed
    If r1.Value = 2 And r2.Value = 0 Then r2.Interior.Color = vbRed
    If r1.Value = 2 And r2.Value = -77 Then r2.Interior.Color = vbRed
    If r1.Value = 2 And r2.Value <> "" And r2.Value <> -66 And r2.Value <> -77 And r2.Value <> 0 And r2.Value <> -99 Then r1.Interior.Color = vbRed
    If r1.Value = 2 And r2.Value <> "" And r2.Value <> -66 And r2.Value <> -77 And r2.Value <> 0 And r2.Value <> -99 Then r2.Interior.Color = vbRed
    If r1.Value = 2 And r2.Value = -99 Then r1.Interior.Color = vbGreen
    If r1.Value = 2 And r2.Value = -99 Then r2.Interior.Color = vbGreen

    If r1.Value = 3 And r2.Value = "" Then r1.Interior.Color = vbRed
    If r1.Value = 3 And r2.Value = -66 Then r1.Interior.Color = vbRed
    If r1.Value = 3 And r2.Value = 0 Then r1.Interior.Color = vbRed
    If r1.Value = 3 And r2.Value = -77 Then r1.Interior.Color = vbRed
    If r1.Value = 3 And r2.Value = "" Then r2.Interior.Color = vbRed
    If r1.Value = 3 And r2.Value = -66 Then r2.Interior.Color = vbRed
    If r1.Value = 3 And r2.Value = 0 Then r2.Interior.Color = vbRed
    If r1.Value = 3 And r2.Value = -77 Then r2.Interior.Color = vbRed
    If r1.Value = 3 And r2.Value <> "" And r2.Value <> -66 And r2.Value <> -77 And r2.Value <> 0 And r2.Value <> -99 Then r1.Interior.Color = vbRed
    If r1.Value = 3 And r2.Value <> "" And r2.Value <> -66 And r2.Value <> -77 And r2.Value <> 0 And r2.Value <> -99 Then r2.Interior.Color = vbRed
    If r1.Value = 3 And r2.Value = -99 Then r1.Interior.Color = vbGreen
    If r1.Value = 3 And r2.Value = -99 Then r2.Interior.Color = vbGreen

    If r1.Value = 4 And r2.Value = "" Then r1.Interior.Color = vbRed
    If r1.Value = 4 And r2.Value = -66 Then r1.Interior.Color = vbRed
    If r1.Value = 4 And r2.Value = 0 Then r1.Interior.Color = vbRed
    If r1.Value = 4 And r2.Value = -77 Then r1.Interior.Color = vbRed
    If r1.Value = 4 And r2.Value = "" Then r2.Interior.Color = vbRed
    If r1.Value = 4 And r2.Value = -66 Then r2.Interior.Color = vbRed
    If r1.Value = 4 And r2.Value = 0 Then r2.Interior.Color = vbRed
    If r1.Value = 4 And r2.Value = -77 Then r2.Interior.Color = vbRed
    If r1.Value = 4 And r2.Value <> "" And r2.Value <> -66 And r2.Value <> -77 And r2.Value <> 0 And r2.Value <> -99 Then r1.Interior.Color = vbRed
    If r1.Value = 4 And r2.Value <> "" And r2.Value <> -66 And r2.Value <> -77 And r2.Value <> 0 And r2.Value <> -99 Then r2.Interior.Color = vbRed
    If r1.Value = 4 And r2.Value = -99 Then r1.Interior.Color = vbGreen
    If r1.Value = 4 And r2.Value = -99 Then r2.Interior.Color = vbGreen

    If r1.Value = 5 And r2.Value = "" Then r1.Interior.Color = vbRed
    If r1.Value = 5 And r2.Value = -66 Then r1.Interior.Color = vbRed
    If r1.Value = 5 And r2.Value = 0 Then r1.Interior.Color = vbRed
    If r1.Value = 5 And r2.Value = -77 Then r1.Interior.Color = vbRed
    If r1.Value = 5 And r2.Value = "" Then r2.Interior.Color = vbRed
    If r1.Value = 5 And r2.Value = -66 Then r2.Interior.Color = vbRed
    If r1.Value = 5 And r2.Value = 0 Then r2.Interior.Color = vbRed
    If r1.Value = 5 And r2.Value = -77 Then r2.Interior.Color = vbRed
    If r1.Value = 5 And r2.Value <> "" And r2.Value <> -66 And r2.Value <> -77 And r2.Value <> 0 And r2.Value <> -99 Then r1.Interior.Color = vbRed
    If r1.Value = 5 And r2.Value <> "" And r2.Value <> -66 And r2.Value <> -77 And r2.Value <> 0 And r2.Value <> -99 Then r2.Interior.Color = vbRed
    If r1.Value = 5 And r2.Value = -99 Then r1.Interior.Color = vbGreen
    If r1.Value = 5 And r2.Value = -99 Then r2.Interior.Color = vbGreen

    If r1.Value = 6 And r2.Value = "" Then r1.Interior.Color = vbRed
    If r1.Value = 6 And r2.Value = -66 Then r1.Interior.Color = vbRed
    If r1.Value = 6 And r2.Value = 0 Then r1.Interior.Color = vbRed
    If r1.Value = 6 And r2.Value = -77 Then r1.Interior.Color = vbRed
    If r1.Value = 6 And r2.Value = "" Then r2.Interior.Color = vbRed
    If r1.Value = 6 And r2.Value = -66 Then r2.Interior.Color = vbRed
    If r1.Value = 6 And r2.Value = 0 Then r2.Interior.Color = vbRed
    If r1.Value = 6 And r2.Value = -77 Then r2.Interior.Color = vbRed
    If r1.Value = 6 And r2.Value <> "" And r2.Value <> -66 And r2.Value <> -77 And r2.Value <> 0 And r2.Value <> -99 Then r1.Interior.Color = vbRed
    If r1.Value = 6 And r2.Value <> "" And r2.Value <> -66 And r2.Value <> -77 And r2.Value <> 0 And r2.Value <> -99 Then r2.Interior.Color = vbRed
    If r1.Value = 6 And r2.Value = -99 Then r1.Interior.Color = vbGreen
    If r1.Value = 6 And r2.Value = -99 Then r2.Interior.Color = vbGreen

    If r1.Value = 7 And r2.Value = "" Then r1.Interior.Color = vbRed
    If r1.Value = 7 And r2.Value = -66 Then r1.Interior.Color = vbRed
    If r1.Value = 7 And r2.Value = 0 Then r1.Interior.Color = vbRed
    If r1.Value = 7 And r2.Value = -77 Then r1.Interior.Color = vbRed
    If r1.Value = 7 And r2.Value = "" Then r2.Interior.Color = vbRed
    If r1.Value = 7 And r2.Value = -66 Then r2.Interior.Color = vbRed
    If r1.Value = 7 And r2.Value = 0 Then r2.Interior.Color = vbRed
    If r1.Value = 7 And r2.Value = -77 Then r2.Interior.Color = vbRed
    If r1.Value = 7 And r2.Value <> "" And r2.Value <> -66 And r2.Value <> -77 And r2.Value <> 0 And r2.Value <> -99 Then r1.Interior.Color = vbRed
    If r1.Value = 7 And r2.Value <> "" And r2.Value <> -66 And r2.Value <> -77 And r2.Value <> 0 And r2.Value <> -99 Then r2.Interior.Color = vbRed
    If r1.Value = 7 And r2.Value = -99 Then r1.Interior.Color = vbGreen
    If r1.Value = 7 And r2.Value = -99 Then r2.Interior.Color = vbGreen

       Next I
       'Error

        End Sub

1 个答案:

答案 0 :(得分:2)

走第一个街区,你可能会做这样的事情:

If r1.Value = 94 Then
   Select case r2.Value
      case " ", "", -99, -66, -77
         r1.Interior.Color = vbRed
         r2.Interior.Color = vbRed
      Case else
         r1.Interior.Color = vbGreen
         r2.Interior.Color = vbGreen
    End Select
ElseIf r1.value = 1 then
   ' repeat the select case or use a function to return the correct colour.
' repeat
End If

如果r2是-99,你的其他区块似乎基本上只关心,在这种情况下使用绿色,否则为红色。