0

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
4

2 回答 2

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
于 2013-07-30T09:32:10.653 回答
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
于 2013-07-30T09:29:49.977 回答