我的循环不会结束

时间:2013-07-30 09:17:51

标签: excel vba excel-vba

1)我的FOR循环中有一个问题。我无限地无限。这是我的第一个问题,我请求你的帮助 2)第二个问题是我不知道如何在“如果”中添加一个以上的条件 我的问题在代码中作为评论。

Sub Repurchase_upload()

Dim Worksheet As Worksheets
startrow = Worksheets("GUTS").Cells(10, 1) 'Here I denote 1
endrow = Worksheets("GUTS").Cells(11, 1) 'Here I denote 1000
For x = startrow To endrow

            If Cells(x, "A").Value <> "DU" Then 'I would like it to look like that: 'If Cells(x, "A").Value <> "DU" or "DR" or "EK" Then' but I don't know how to do this 'or'
            Cells(x, "A").EntireRow.ClearContents
            End If 'And here it won't end...
    Next
End Sub

2 个答案:

答案 0 :(得分:2)

进行多值测试的另一种方法是使用select语句,如下所示:

Sub Repurchase_upload()

    Dim Worksheet As Worksheets
    startrow = Worksheets("GUTS").Cells(10, 1) 'Here I denote 1
    endrow = Worksheets("GUTS").Cells(11, 1) 'Here I denote 1000
    For x = startrow To endrow
        Select Case Cells(x, "A").Value
        Case "DU", "DR", "EK"
          'Do nothing
        Case Else
            Cells(x, "A").EntireRow.ClearContents
        End Select
    Next
End Sub

答案 1 :(得分:1)

多条件易于实现:

For x = startrow To endrow
    If Cells(x, "A").Value <> "DU" Or Cells(x, "A").Value <> "DR" Or Cells(x, "A").Value <> "EK" Then 
        Cells(x, "A").EntireRow.ClearContents
    End If
Next x

对于我不知道的无限循环,对我来说似乎很好......不是太慢了吗? 也许你应该在调用ClearContents一千次之前禁用屏幕更新,例如:

Sub Repurchase_upload()

    Application.ScreenUpdating = False 'Disable screen updating

    Dim Worksheet As Worksheets
    startrow = Worksheets("GUTS").Cells(10, 1) 'Here I denote 1
    endrow = Worksheets("GUTS").Cells(11, 1) 'Here I denote 1000
    For x = startrow To endrow
        If Cells(x, "A").Value <> "DU" Or Cells(x, "A").Value <> "DR" Or Cells(x, "A").Value <> "EK" Then 
            Cells(x, "A").EntireRow.ClearContents
        End If
    Next x

    Application.ScreenUpdating = True  'Re-enable screen updating

End Sub