有没有办法在嵌套 if 循环中使用 "FOR" 语句?
Is there a way to have a "FOR" statement within a nested-if loop?
我正在尝试删除工作簿中与用户输入的值匹配的 sheet。如果该值不存在,则显示一条错误消息。有没有比我目前拥有的更好的方法来做到这一点?
最初,我尝试了一个带有多个 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
如果 sheet 存在,我希望将其删除,但无法使 if 语句和 FOR 循环起作用。
试一试,如有任何问题,请告诉我。我没有过多评论,因为代码流应该是不言自明的。
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
我认为您看到了这两条消息,因为工作表在第一个循环中而不是在第二个循环中。为什么不只循环一次并使用 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
我正在尝试删除工作簿中与用户输入的值匹配的 sheet。如果该值不存在,则显示一条错误消息。有没有比我目前拥有的更好的方法来做到这一点?
最初,我尝试了一个带有多个 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
如果 sheet 存在,我希望将其删除,但无法使 if 语句和 FOR 循环起作用。
试一试,如有任何问题,请告诉我。我没有过多评论,因为代码流应该是不言自明的。
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
我认为您看到了这两条消息,因为工作表在第一个循环中而不是在第二个循环中。为什么不只循环一次并使用 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