自动填充MsgBox

时间:2017-05-26 14:53:43

标签: vba excel-vba excel-2013 msgbox excel

我想知道是否有人可以帮助我将下面变成一个msg框。基本上我希望它首先检查以下任何一个范围是否说检查,如果他们这样做,msgbox会出现解释哪个规则已被破坏。我曾试图创建变量,但不确定如何在msgbox中实现这一点。

Sub ErrorMsgBox()

Dim Error1 As String
    If Range("DaisyFreshRule").Value = "CHECK" Then
    Error1 = "Daisy Fresh Rule"
Else
End If

Dim Error2 As String
    If Range("MigrationRule").Value = "CHECK" Then
    Error2 = "Migration Rule"
Else
End If

Dim Error3 As String
    If Range("ServiceCreditRule").Value = "CHECK" Then
    Error3 = "Service Credit Rule"
Else
End If

End Sub

2 个答案:

答案 0 :(得分:6)

Sub ErrorMsgBox()

  Dim Error As String: Error=""

  If Range("DaisyFreshRule").Value = "CHECK" Then
    Error = vbNewLine & "Daisy Fresh Rule"
  End If

  If Range("MigrationRule").Value = "CHECK" Then
    Error = Error & vbNewLine & "Migration Rule"
  End If

  If Range("ServiceCreditRule").Value = "CHECK" Then
    Error = Error & vbNewLine & "Service Credit Rule"
  End If

  If Error <> "" Then
    MsgBox "The following checks have been detected:" & Error
  End If

End Sub

答案 1 :(得分:3)

试一试

Sub ErrorMsgBox()
    Dim Err As String

    If UCase(Range("DaisyFreshRule").Value) = "CHECK" Then Err = Err & "Daisy Fresh Rule" & vbNewLine
    If UCase(Range("MigrationRule").Value) = "CHECK" Then Err = Err & "Migration Rule" & vbNewLine
    If UCase(Range("ServiceCreditRule").Value) = "CHECK" Then Err = Err & "Service Credit Rule" & vbNewLine

    If Not Err = vbNullString Then MsgBox prompt:=Err, Buttons:=vbCritical
End Sub