如何在满足条件时显示不同的消息框

How to display different message box when meet condition

我希望在满足以下要求时显示不同的消息框:

  1. 当textbox8的值小于3时,消息框会显示textbox8超出范围,用户是否要继续存储数据,如果用户点击是,则继续存储数据,但如果用户select 否,将要求用户重新输入 textbox8 值然后仅存储数据。

  2. 当textbox8值在3到3.2之间时,消息框会显示textbox8在3到3.2之间,请注意,并显示一个确定按钮供用户点击并存储数据。

我尝试在里面添加另一个消息框,但它只会检查第一个条件。

Sheets("Overall").Activate    
With Me
    If Len(.ComboBox5.Value) * Len(.TextBox4.Value) * Len(.TextBox5.Value) * Len(.TextBox6.Value) * Len(.ComboBox6.Value) * Len(.TextBox7.Value) * Len(.TextBox8.Value) = 0 Then
        MsgBox "Please Complete All Fields Before Submit"
    Else

        If CSng(.TextBox8.Text) < 3 Then
            If MsgBox("TextBox8 less than 3.0" & vbLf & vbLf & _
                      "Do you wish to continue?", vbYesNo, "Exceeds") = vbNo Then

                MsgBox "user to re-type the value in TextBox8.", vbInformation, "Title"

                TextBox8.SetFocus

                          Else

        If CSng(.TextBox8.Text) >= 3 And CSng(.TextBox8.Text) <= 3.2 Then

        MsgBox "TextBox8 between 3 to 3.2, Aware!!!", , "Alert"


                  Exit Sub

         End If
         End If
         End If
        eRow = Sheet4.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
               Cells(eRow, 11).Value = ComboBox5.Text
               Cells(eRow, 7).Value = TextBox4.Text
               Cells(eRow, 8).Value = TextBox5.Text
               Cells(eRow, 14).Value = TextBox6.Text
               Cells(eRow, 16).Value = ComboBox6.Text
               Cells(eRow, 12).Value = TextBox7.Text
               Cells(eRow, 13).Value = TextBox8.Text
               Cells(eRow, 19).Value = TextBox9.Text

    End If
End With
End Sub

这行代码 If CSng(.TextBox8.Text) >= 3 And CSng(.TextBox8.Text) <= 3.2 Then 不需要在 If CSng(.TextBox8.Text) < 3 Then 代码块内。如果您更改代码并修复缩进,则代码如下所示:

    Sheets("Overall").Activate
    With Me
        If Len(.ComboBox5.Value) * Len(.TextBox4.Value) * Len(.TextBox5.Value) * Len(.TextBox6.Value) * Len(.ComboBox6.Value) * Len(.TextBox7.Value) * Len(.TextBox8.Value) = 0 Then
            MsgBox "Please Complete All Fields Before Submit"
        Else        ' If Len(.ComboBox5.Value) * Len(.TextBox4.Value)
            If CSng(.TextBox8.Text) < 3 Then
                If MsgBox("TextBox8 less than 3.0" & vbLf & vbLf & _
                          "Do you wish to continue?", vbYesNo, "Exceeds") = vbNo Then

                    MsgBox "user to re-type the value in TextBox8.", vbInformation, "Title"
                    TextBox8.SetFocus

                End If        ' If MsgBox("TextBox8 less than 3.0"
             End If        ' If CSng(.TextBox8.Text) < 3 Then

            If CSng(.TextBox8.Text) >= 3 And CSng(.TextBox8.Text) <= 3.2 Then

                MsgBox "TextBox8 between 3 to 3.2, Aware!!!", , "Alert"
                Exit Sub

            End If        ' If CSng(.TextBox8.Text) >= 3 And

            eRow = Sheet4.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
                   Cells(eRow, 11).Value = ComboBox5.Text
                   Cells(eRow, 7).Value = TextBox4.Text
                   Cells(eRow, 8).Value = TextBox5.Text
                   Cells(eRow, 14).Value = TextBox6.Text
                   Cells(eRow, 16).Value = ComboBox6.Text
                   Cells(eRow, 12).Value = TextBox7.Text
                   Cells(eRow, 13).Value = TextBox8.Text
                   Cells(eRow, 19).Value = TextBox9.Text

        End If        ' If Len(.ComboBox5.Value) * Len(.TextBox4.Value)
    End With
End Sub

如果您发现代码中有很多 If 块,您还可以在 ElseEnd If 行中添加注释以显示哪个 If 语句他们属于。我已经在上面的代码中完成了。