为每个事件发生组合在一个msgbox中的多个消息框

时间:2018-06-11 13:08:14

标签: vba excel-vba excel

我正在尝试在BeforeSave事件中输出特定的错误消息。以下是示例消息框:http://prntscr.com/jtlxt2。  对于每个用例,必须使用来自特定测试用例的已定义错误消息替换该部件。如果存在多个问题,则应在一个消息框中打印所有错误消息。以下是可能的错误消息:

1.删除blockTemplate的ID

2.必须定义参数“ID”

3.单元格B2不允许为空

4.Cell A2包含无效值:“Ids”

5.Font Size必须是6到72之间的整数

6.Paragraph Spacing Before必须是6到72之间的整数 字体大小必须是6到72之间的整数

表“列变体”:

7.变体ID QINTRO_VAR1,QINTRO_VAR2与全局ID QUINTRO不兼容

8.Cell C6不允许为空。要为此值定义null,请使用减号( - )。

这是我到目前为止编写的代码:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

Application.EnableEvents = False
Dim cell As Range
Dim j As String
Dim i As Integer
Dim cellVal As Integer
Dim cellVal2 As Integer
Dim sCellVal As String
Dim a As Variant
Dim Target As Range
Dim arr As Range
Dim rngcheck As Range
Dim rngcheck2 As Range

sCellVal = Range("A2").Value
cellVal = Range("B3").Value
cellVal2 = Range("B4").Value

   If Not IsNumeric(cellVal) Then
   MsgBox "Only numeric values allowed."
   End If

  'If Sheets("General Info").Range("A2").Value = "" Then
  'Cancel = True
  'MsgBox "Save cancelled"
  'End If

  If Not sCellVal = "ID" Then
      Cancel = True
      MsgBox "The Parameter “ID” must be defined"
  End If

  If sCellVal = "" Then
      Cancel = True
  MsgBox "Missing ID for the blockTemplate"
  End If

  If sCellVal = "IDs" Then
      'Cancel = True
       MsgBox "Cell A2 contains an invalid value: “Ids”"
  End If

  If Not cellVal = (6 < 72) Then
      MsgBox "Font Size must be an integer from 6 till 72"
  End If

  If Not cellVal2 = (6 < 72) Then
      MsgBox "Paragraph Spacing Before must be an integer from 6 till 72"
  End If

   'Set arr = Range("C6:C7")
    'If the columns is the eighth
    'For Each a In arr
     'ActiveSheet.Range("C6:C7").Select
    'If Target.Column = 2 And (Target.Row > 5 And Target.Row < 8) Then


          Set rngcheck2 = Range("C6:C7")

       For Each cell In rngcheck2
    If IsEmpty(cell) Then


          MsgBox (" The cell" + Target.Address(0, 0)) + "is not allowed to be empty. To define null for this value use the minus sign (-)."
     'The Cell C6 is not allowed to be empty. To define null for this value use the minus sign (-).
    End If
    Next cell



        'Next a:
          MsgBox (" The Variant IDs QINTRO_VAR1, QINTRO_VAR2 are not compatible with the global ID QUINTRO")

        Set rngcheck = Range("B2:B4")

         i = 0

          For Each cell In rngcheck


        If IsEmpty(cell) Then

           i = i + 1

          j = j & cell.Address & vbNewLine

        End If

          Next cell

         If i = 0 Then Exit Sub

              MsgBox "Sorry, you must enter a value in: " & vbNewLine & j

         Application.EnableEvents = True

End Sub

3 个答案:

答案 0 :(得分:2)

我会构建一条消息,然后只输出一次。像:

Sub whatever()
    Dim mess As String
    mess = ""

  If Not sCellVal = "ID" Then
      Cancel = True
      mess = mess & vbCrLf & "The Parameter “ID” must be defined"
  End If

  If sCellVal = "" Then
      Cancel = True
      mess = mess & vbCrLf & "Missing ID for the blockTemplate"
  End If

  ' more code

  If mess <> "" Then MsgBox mess
End Sub

答案 1 :(得分:1)

创建一个名为errorString的String。

将现有的“MsgBox”替换为“errorString = errorString&amp; vbCrLf”

在例程结束时检查是否有任何错误(errorString包含某些内容)然后只是msgbx errorString

If len(errorString)>0 Then
    errorString = "Please correct the following Errors before continuing" & errorString (or whatever)
endif

答案 2 :(得分:0)

好的......这不是很好,而且部分是因为VBA想逐行...所以每个错误消息都需要有自己的块,例如:

Dim as as string, b as string, c as string
If sCellVal = "ID" Then a = "The Parameter “ID” must be defined."
If sCellVal = "" Then b = "  Missing ID for the blockTemplate."
If sCellVal = "IDs" Then c = "  Cell A2 contains an invalid value: “Ids.”"

MsgBox a & b & c 'Note that I put 2 spaces in front of the text above

您需要将使用Cancel = True的操作分组到一个分组中,并将非Cancel = True阻塞到他们自己的分组中。我建议第二个出现Cancel = True块,这样你就可以收集所有可能的错误信息。