Excel VBA错误:无Do循环

时间:2016-06-14 21:45:14

标签: excel-vba vba excel

嵌套的If语句出错,导致Do循环错误。如果我将If块简化为一个项目,它可以正常工作。 (我通常在VS中使用C#.NET)。这应该是Excel中一个简单表单的一部分,以帮助数据输入。

Private Sub cbDelete_Click()
If tbName.Value = "" Then
MsgBox "Sorry, please navigate to a non-blank row."
Exit Sub
End If

 Dim i As Integer
i = 3
Do While ThisWorkbook.Worksheets("Non-SR").Range("A" & i).Value <> ""

'MsgBox ThisWorkbook.Worksheets("Non-SR").Range("A" & i).Value

If (tbName.Value = ThisWorkbook.Worksheets("Non-SR").Range("A" & i).Value) Then
     If (dpDateSubmited.Value = ThisWorkbook.Worksheets("Non-SR").Range("B" & i).Value) Then
        If (tbLocation.Value = ThisWorkbook.Worksheets("Non-SR").Range("C" & i).Value) Then
            If (tbBU.Value = ThisWorkbook.Worksheets("Non-SR").Range("D" & i).Value) Then
                If (tbTitle.Value = ThisWorkbook.Worksheets("Non-SR").Range("E" & i).Value) Then
                    If (tbDescription.Value = ThisWorkbook.Worksheets("Non-SR").Range("F" & i).Value) Then
                        If (tbStatus.Value = ThisWorkbook.Worksheets("Non-SR").Range("G" & i).Value) Then
                            ThisWorkbook.Worksheets("Non-SR").Rows(i).Delete Shift:=xlUp
                            Exit Sub
End If
i = i + 1
Loop

MsgBox "Item not found!"
End Sub


New fixed code:
Private Sub CommandButton1_Click()
If tbName.Value = "" Then
MsgBox "Sorry, please navigate to a non-blank row."
Exit Sub
End If

Dim i As Integer
i = 3
Do While ThisWorkbook.Worksheets("Non-SR").Range("A" & i).Value <> ""

'MsgBox ThisWorkbook.Worksheets("Non-SR").Range("A" & i).Value

If (tbName.Value = ThisWorkbook.Worksheets("Non-SR").Range("A" & i).Value) And _
     (dpDateSubmited.Value = ThisWorkbook.Worksheets("Non-SR").Range("B" & i).Value) And _
         (tbLocation.Value = ThisWorkbook.Worksheets("Non-SR").Range("C" & i).Value) And _
             (tbBU.Value = ThisWorkbook.Worksheets("Non-SR").Range("D" & i).Value) And _
                 (tbTitle.Value = ThisWorkbook.Worksheets("Non-SR").Range("E" & i).Value) And _
                     (tbDescription.Value = ThisWorkbook.Worksheets("Non-SR").Range("F" & i).Value) And _
                         (tbStatus.Value = ThisWorkbook.Worksheets("Non-SR").Range("G" & i).Value) Then
                            ThisWorkbook.Worksheets("Non-SR").Rows(i).Delete Shift:=xlUp
                            Exit Sub
End If
i = i + 1
Loop

MsgBox "Item not found!"
End Sub

感谢所有帮助。 -RickH

2 个答案:

答案 0 :(得分:0)

我还不太确定,你需要什么。不过,我想提出一些改进,以便通过以下方式简化代码块:

Private Sub cbDelete_Click()

If tbName.Value = "" Then
    MsgBox "Sorry, please navigate to a non-blank row."
    Exit Sub
End If

Dim i As Integer

i = 3
With ThisWorkbook.Worksheets("Non-SR")
    For i = 3 To .Cells(.Rows.Count, "A").End(xlUp).Row
        'MsgBox ThisWorkbook.Worksheets("Non-SR").Range("A" & i).Value
        If tbName.Value = .Range("A" & i).Value And _
            dpDateSubmited.Value = .Range("B" & i).Value And _
            tbLocation.Value = .Range("C" & i).Value And _
            tbBU.Value = .Range("D" & i).Value And _
            tbTitle.Value = .Range("E" & i).Value And _
            tbDescription.Value = .Range("F" & i).Value And _
            tbStatus.Value = .Range("G" & i).Value Then
                .Rows(i).Delete Shift:=xlUp
                Exit Sub
        End If
    Next i
End With

MsgBox "Item not found!"

End Sub
  1. Do...Loop已被移除并与For...Next
  2. 交换
  3. 所有If语句已合并为一个
  4. 已设置With块以加快代码速度并缩短代码。

答案 1 :(得分:0)

如果语句是代码块。
您可以使用单行If语句

<强> VBA

If a = 10 Then do_something Else do_somthingElse

C#

if(a = 10)      
   do_somthingElse;

多行If语句必须关闭

VBA 使用结束语如果要包含代码

If a = 10 Then 
    do_something 
Else 
   do_somthingElse
End If

C#使用方括号{}括起代码

if(a = 10){
  do_somthingElse;
else{
  do_somthingElse; 
}

您的代码已打开7 If语句阻止其中一个已关闭