如果条件为for循环

时间:2016-12-27 09:29:08

标签: excel-vba vba excel

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

以上代码仅适用于X2,但我希望在X52之前完成。

(例如:下一次检查X3 =完成“或范围(”X3“)=”取消“然后         范围( “X3”)。选择         ActiveCell.FormulaR1C1 =“= GetOrderStatus(RC [1])”并执行操作,接下来是X4,依此类推

2 个答案:

答案 0 :(得分:1)

我认为在你的帖子中你的意思是:

  1. 不是范围(" X2")="完成" >> 也可以替换为Range(" X2")<> " COMPLETE"
  2. 不是范围(" X2")="取消" >> 也可以替换为Range(" X2")<> "取消"
  3. 注意:最好远离ActiveCellRange,而是使用引用的Range("X" & i).FormulaR1C1。在您的代码中,您可以直接使用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

    进行编码

    <强>代码

    from itertools import cycle
    
    with open('test_file','r') as tf, open('keyword_file', 'r') as kf:
        keywords = [k.strip() for k in kf]
        for lineno, line in enumerate(tf):
            for i, key in enumerate(cycle(keywords)):
                if i==len(keywords):
                    break
                if key in line.strip():
                    print "Keyword: '"+key+"' found on line: "+str(lineno)
    

答案 1 :(得分:0)

使用行/列号

x是第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