VBA - 在 msgbox 后返回用户窗体

VBA - Go back to Userform after msgbox

我有一个用户窗体,其中目前有 15 个组合框。允许用户 select 本季度、上季度和去年的 5 份报告。由于这是 select 来自的很多框,我认为应用一些错误处理以避免遗漏任何框会很好。

我的代码是这样的(显然每个盒子都重复):

If IsNull(UF_RM_Reports.Report1.Value) Then

MsgBoxResult = MsgBox("No report selected for Report 1 current quarter, is this correct?", vbYesNo + vbQuestion, "Report Template")

    If MsgBoxResult = vbNo Then
        End

    Else

    End If

End If

我遇到的问题是,如果用户意识到他们还没有 select 编辑报告并按“不,我已经结束”,这不仅会结束宏,还会关闭组合框。如果您随后重新打开它,所有 selection 都将消失。有点破坏错误处理的意义,因为他们需要重新开始。

我想知道是否有什么我可以更改 End 从停止代码但允许用户返回并 select 丢失的报告。

提前致谢

如果要求是让用户注意到 he/she 错过了报告选择这一事实,为什么不尝试在任何一个事件中摆弄组合框的颜色 属性 - 东西喜欢?

  1. 在 useform 激活事件中设置组合框的颜色状态

    Private Sub UserForm_Activate()
    
      ComboBox1.BackColor = vbRed
      UserForm1.ComboBox1.AddItem "A"
      UserForm1.ComboBox1.AddItem "B"
      UserForm1.ComboBox1.AddItem "C"
    
    End Sub
    
  2. 然后在组合框更改事件中将颜色改回原来的颜色

    Private Sub ComboBox1_Change()
         If ComboBox1.value = "" Then
            ComboBox1.BackColor = vbRed
         Else
           ComboBox1.BackColor = &H80000005
         End If
    End Sub
    

有几种方法可以处理这种情况。由于我不知道您的用户窗体的详细信息,因此我将概述一般方法。我倾向于在按下用户窗体上的按钮时验证控件,这表明用户已准备好生成报告:

Private Sub CommandButton1_Click()
   If Trim(Report1.Value) = "" Then
      If MsgBox("No report selected for Report 1...", vbYesNo + vbQuestion, "Report Template") = vbNo Then
         Report1.SetFocus
         Exit Sub
      End If
   End If

   If Trim(Report2.Value) = "" Then
      If MsgBox("No report selected for Report 2...", vbYesNo + vbQuestion, "Report Template") = vbNo Then
         Report2.SetFocus
         Exit Sub
      End If
   End If

   'create the reports when nothing has been missed or
   'the user wants to proceed anyway
   MsgBox "create reports"
End Sub

因为有很多控件,你可以用一个循环来完成同样的事情:

Private Sub CommandButton1_Click()
   Dim c As Control
   
   For Each c In Me.Controls
      If TypeOf c Is ComboBox Then
         If Trim(c.Value) = "" Then
            If MsgBox("You have not selected all reports.  Is this correct?", vbYesNo + vbQuestion, "Report Template") = vbNo Then
               Report1.SetFocus
               Exit Sub
            Else
               Exit For
            End If
         End If
      End If
   Next
   
   'create the reports when nothing has been missed or
   'the user wants to proceed anyway
   MsgBox "create reports"
End Sub