2016-07-17 4 views
1

私の列の各行を通過するので、可能であれば、下にあるIf文を高速化しようとしています。これは私のマクロの実行時間に影響を与える:if文を高速化したい場合

Set Col = Range("O2:O" & lastrowOU3) 
For Each Cell In Col 
If Cell.Offset(0, -2) = 0 And Cell.Offset(0, -1) = 0 Then Cell.Value = "NEW ITEM" 
If Cell.Offset(0, -2) = 0 And Cell.Offset(0, -1) = "NEW ITEM" Then Cell.Value = "NOT VALIDATED" 
If Cell.Offset(0, -2) < Date And Cell.Offset(0, -1) = "NEW ITEM" Then Cell.Value = "NOT VALIDATED" 
If Cell.Offset(0, -2) < Date And Cell.Offset(0, -1) = "VALIDATED" Then Cell.Value = "NOT VALIDATED" 
If Cell.Offset(0, -2) < Date And Cell.Offset(0, -1) = "NOT VALIDATED" Then Cell.Value = "NOT VALIDATED 1" 
If Cell.Offset(0, -2) < Date And Cell.Offset(0, -1) = "NOT VALIDATED 1" Then Cell.Value = "NOT VALIDATED 2" 
If Cell.Offset(0, -2) >= Date Then Cell.Value = "VALIDATED" 
Next 

は、上記のようにマクロがまっすぐ次の行に行く満たされるの後、各シナリオを経由回避する方法はありますか?それとももっと速い方法でしょうか?

おかげで、

コナー「のElseIf」を使用して

+0

1.)IF/ELSEIF/ENDIF 2.)変数を変数に代入すると、ループごとに2回だけシートに移動します。 –

+0

さらに速くしたい場合は、VBAの代わりにExcelの式を使用してください。 – Slai

答えて

2

は、前の手順が満たされていない場合のテストにのみ行われることを意味します。また、もう少し物事をスピードアップします文をネスト

Set Col = Range("O2:O" & lastrowOU3) 
For Each Cell In Col 
If Cell.Offset(0, -2) = 0 And Cell.Offset(0, -1) = 0 Then 
    Cell.Value = "NEW ITEM" 
ElseIf Cell.Offset(0, -2) = 0 And Cell.Offset(0, -1) = "NEW ITEM" Then 
    Cell.Value = "NOT VALIDATED" 
ElseIf Cell.Offset(0, -2) < Date And Cell.Offset(0, -1) = "NEW ITEM" Then 
    Cell.Value = "NOT VALIDATED" 
ElseIf Cell.Offset(0, -2) < Date And Cell.Offset(0, -1) = "VALIDATED" Then 
    Cell.Value = "NOT VALIDATED" 
ElseIf Cell.Offset(0, -2) < Date And Cell.Offset(0, -1) = "NOT VALIDATED" Then 
    Cell.Value = "NOT VALIDATED 1" 
ElseIf Cell.Offset(0, -2) < Date And Cell.Offset(0, -1) = "NOT VALIDATED 1" Then 
    Cell.Value = "NOT VALIDATED 2" 
ElseIf Cell.Offset(0, -2) >= Date Then 
    Cell.Value = "VALIDATED" 
End If 
Next 

Set Col = Range("O2:O" & lastrowOU3) 
For Each Cell In Col 
If Cell.Offset(0, -2) = 0 Then 
    IF And Cell.Offset(0, -1) = 0 Then 
     Cell.Value = "NEW ITEM" 
    ElseIf Cell.Offset(0, -1) = "NEW ITEM" Then 
     Cell.Value = "NOT VALIDATED" 
    End If 
ElseIf Cell.Offset(0, -2) < Date Then 
    If Cell.Offset(0, -1) = "NEW ITEM" Then 
     Cell.Value = "NOT VALIDATED" 
    ElseIf Cell.Offset(0, -1) = "VALIDATED" Then 
     Cell.Value = "NOT VALIDATED" 
    ElseIf Cell.Offset(0, -1) = "NOT VALIDATED" Then 
     Cell.Value = "NOT VALIDATED 1" 
    ElseIf Cell.Offset(0, -1) = "NOT VALIDATED 1" Then 
     Cell.Value = "NOT VALIDATED 2" 
    End If 
ElseIf Cell.Offset(0, -2) >= Date Then 
    Cell.Value = "VALIDATED" 
End If 
Next 

をそして、それぞれの「Cell.Offset、おそらくもっとそれをスピードアップしますSELECT CASE文を使用するなど(0、x)を「一度だけ評価される必要があります(私は思う):

Set Col = Range("O2:O" & lastrowOU3) 
For Each Cell In Col 
Select Case Cell.Offset(0, -2) 
    Case 0 
     Select Case Cell.Offset(0, -1) 
      Case 0 
       Cell.Value = "NEW ITEM" 
      Case "NEW ITEM" 
       Cell.Value = "NOT VALIDATED" 
      Case Else 
     End Select 
    Case < Date 
     Select Case Cell.Offset(0, -1) 
      Case "NEW ITEM" 
       Cell.Value = "NOT VALIDATED" 
      Case "VALIDATED" 
       Cell.Value = "NOT VALIDATED" 
      Case "NOT VALIDATED" 
       Cell.Value = "NOT VALIDATED 1" 
      Case "NOT VALIDATED 1" 
       Cell.Value = "NOT VALIDATED 2" 
      Case Else 
     End Select 
    Case Else 
     Cell.Value = "VALIDATED" 
End Select 
Next 
+0

乾杯してください。ほぼそこにいた。日曜日にあなたの助けを感謝します! – Conor

0

最低限:

Sub dural() 
    Set Col = Range("O2:O" & lastrowOU3) 
    Dim v1 As Variant, v2 As Variant, s As String 

    For Each cell In Col 
     v1 = cell.Offset(0, -2).Value 
     v2 = cell.Offset(0, -1).Value 
     If v1 = 0 And v2 = 0 Then s = "NEW ITEM" 
     If v1 = 0 And v2 = "NEW ITEM" Then s = "NOT VALIDATED" 
     If v1 < Date And v2 = "NEW ITEM" Then s = "NOT VALIDATED" 
     If v1 < Date And v2 = "VALIDATED" Then s = "NOT VALIDATED" 
     If v1 < Date And v2 = "NOT VALIDATED" Then s = "NOT VALIDATED 1" 
     If v1 < Date And v2 = "NOT VALIDATED 1" Then s = "NOT VALIDATED 2" 
     If v1 >= Date Then s = "VALIDATED" 
     cell.Value = s 
    Next 
End Sub 
関連する問題