【问题标题】:Is there a way to have a "FOR" statement within a nested-if loop?有没有办法在嵌套 if 循环中使用“FOR”语句?
【发布时间】:2020-01-06 14:52:45
【问题描述】:

如果工作簿中的工作表与用户输入的值匹配,我将尝试删除它。如果该值不存在,则显示错误消息。有没有比我目前拥有的更好的方法来做到这一点?

最初,我尝试了一个带有多个 if 语句的嵌套 if 循环来获得我所取得的结果,但这没有奏效。下面的代码是我得到的,但它仍然有一些错误。例如,当满足第一个 FOR 条件时,两个 FOR 语句都会出现消息框(这是不正确的)。

Dim ws As Worksheet

Private Sub cmdDeleteProj_Click()
'
'Remove project sheet after project has been completed
'

'Show error message if user input field is blank
If txtRemProjNum.Value = "" Then
    MsgBox "Input valid project number to continue.", vbExclamation, "Required Field Left Blank"
    uf_RemProj.txtRemProjNum.SetFocus
End If

'Delete user defined project sheet if it exists
On Error Resume Next
For Each ws In ActiveWorkbook.Worksheets
    If ws.Name = txtRemProjNum Then
        Application.DisplayAlerts = False
        ws.Delete
        Application.DisplayAlerts = True
        MsgBox "Project removed from inventory tracker."
    End If
Next ws

'Show error message if project does not exist
On Error Resume Next
For Each ws In ActiveWorkbook.Worksheets
    If txtRemProjNum.Value <> "" And ws.Name <> txtRemProjNum Then
        MsgBox "Project number not found." & vbNewLine & "" & vbNewLine & "Input valid project number.", vbCritical, "Out of Range"
        uf_RemProj.txtRemProjNum.Text = ""
        uf_RemProj.txtRemProjNum.SetFocus
    End If
Next ws

End Sub

如果工作表存在,我希望将其删除,但无法使 if 语句和 FOR 循环工作。

【问题讨论】:

    标签: excel vba for-loop nested-loops


    【解决方案1】:

    试一试,如有任何问题,请告诉我。我没有过多评论,因为代码流应该是不言自明的。

    Option Explicit
    
    Dim ws As Worksheet
    
    Private Sub cmdDeleteProj_Click()
    
        'Remove project sheet after project has been completed
    
        'Show error message if user input field is blank
        If txtRemProjNum.Value = "" Then
            MsgBox "Input valid project number to continue.", vbExclamation, "Required Field Left Blank"
            uf_RemProj.txtRemProjNum.SetFocus
            Exit Sub
        End If
    
        Dim projectFound As Boolean
    
        'Delete user defined project sheet if it exists
        For Each ws In ThisWorkbook.Worksheets
            If ws.Name = txtRemProjNum Then
                Application.DisplayAlerts = False
                ws.Delete
                Application.DisplayAlerts = True
                MsgBox "Project removed from inventory tracker."
                projectFound = True
                Exit For
            End If
        Next ws
    
        If Not projectFound Then
            MsgBox "Project number not found." & vbNewLine & "" & vbNewLine & "Input valid project number.", vbCritical, "Out of Range"
            uf_RemProj.txtRemProjNum.Text = ""
            uf_RemProj.txtRemProjNum.SetFocus
        End If
    
    End Sub
    

    【讨论】:

      【解决方案2】:

      我认为您看到这两条消息是因为工作表在第一个循环中而不是在第二个循环中。为什么不只循环一次并使用elseif 语句

      For Each ws In ActiveWorkbook.Worksheets
          If ws.Name = txtRemProjNum Then
              Application.DisplayAlerts = False
              ws.Delete
              Application.DisplayAlerts = True
              MsgBox "Project removed from inventory tracker."
          ElseIf
              txtRemProjNum.Value <> "" And ws.Name <> txtRemProjNum Then
              MsgBox "Project number not found." & vbNewLine & "" & vbNewLine & "Input 
                      valid project number.", vbCritical, "Out of Range"
              uf_RemProj.txtRemProjNum.Text = ""
              uf_RemProj.txtRemProjNum.SetFocus
          End If
      Next ws
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多