If condition in for loop

2019-09-07 11:55发布

Sub NewRefesh()
    If Not Range("X2") = "COMPLETE" Or Range("X2") = "CANCELLED" Then
        Range("X2").Select
        ActiveCell.FormulaR1C1 = "=GetOrderStatus(RC[1])"
    End If
End Sub

The above code does for only X2 but i want do it till X52.

(Ex: Next check in X3 = COMPLETE" Or Range("X3") = "CANCELLED" Then Range("X3").Select ActiveCell.FormulaR1C1 = "=GetOrderStatus(RC[1])"and do the action, next X4 and so on

2条回答
Bombasti
2楼-- · 2019-09-07 12:22

I think in your post you meant your criteria to be :

  1. Not Range("X2") = "COMPLETE" >> can be replaced also with Range("X2") <> "COMPLETE"
  2. Not Range("X2") = "CANCELLED" >> can be replaced also with Range("X2") <> "CANCELLED"

Note: it's better to stay away from Select and ActiveCell, instead use referenced Ranges. In your code you code directly use Range("X" & i).FormulaR1C1

Code

Option Explicit

Sub NewRefesh()

    Dim i As Long

    ' simple For loop, you can modify to find last row with data instead of 52
    For i = 2 To 52
        If (Not Range("X" & i).Value = "COMPLETE") And (Not Range("X" & i).Value = "CANCELLED") Then
            Range("X" & i).FormulaR1C1 = "=GetOrderStatus(RC[1])"
        End If
    Next i


End Sub
查看更多
倾城 Initia
3楼-- · 2019-09-07 12:28

use row/col numbering

x is Column number 24

for i = 2 to 52
If Not cells(i,24) = "COMPLETE" Or cells(i,24) = "CANCELLED" Then
        Range(i,24).Select
        Whatever you want done.........
    End If
Next i
查看更多
登录 后发表回答