无论如何VBA都在优化这段代码?

时间:2017-01-25 13:52:59

标签: vba optimization

目前处理需要我使用以下代码的内容。只是想知道是否有任何方式缩短它或将它组合起来。任何帮助都会很棒。并且抱歉在过去几天发布了这么多问题。

Sub RoutingCheck()


   Dim i As Long, c1 As Range, c2 As Range, c3 As Range, c4 As Range

   For i = 2 To 456
      Set c3 = Range("C" & i)
      Set c4 = Range("D" & i)

If c3.Value <> 1 Or c3.Value <> 2 Or c3.Value <> 3 Or c3.Value <> 4 Or c3.Value <> 5 Or c3.Value <> 6 Or c3.Value <> 98 Then c3.Interior.color = vbRed
If c3.Value = 1 Or c3.Value = 2 Or c3.Value = 3 Or c3.Value = 4 Or c3.Value = 5 Or c3.Value = 6 Or c3.Value = 98 Then c3.Interior.color = vbGreen


End If
Next i

End Sub

1 个答案:

答案 0 :(得分:1)

正是Select Case的全部内容,请尝试以下代码:

Sub RoutingCheck()

Dim i As Long, c1 As Range, c2 As Range, c3 As Range, c4 As Range

For i = 2 To 456
    Set c3 = Range("C" & i)
    Set c4 = Range("D" & i)

    Select Case c3.Value
        Case 1 To 6
            c3.Interior.Color = vbGreen
        Case Else
            c3.Interior.Color = vbRed
    End Select

Next i

End Sub