【问题标题】:Pop Out box if the data is not within the range如果数据不在范围内,则弹出框
【发布时间】:2015-05-29 18:00:11
【问题描述】:

我在我的 excel 工作簿中开发了一个用户表单。

我希望在满足以下要求时点击提交按钮时弹出警告消息:

  1. 如果combobox2的值等于Ni,combobox6的值需要在50T、30U、20A和100​​以内

  2. 如果combobox6中的值不在这些值之内,当用户点击提交按钮时,它会显示一个消息框提醒用户combobox6的值不在规格范围内,如果用户选择是,那么excel会存储数据,如果用户选择否,用户将需要重新选择数据。

我的用户表单代码将如下所示:

Private Sub CommandButton1_Click()    
Sheets("Overall").Activate

With Me
    If Len(.ComboBox1.Value) * Len(.TextBox2.Value) * Len(.TextBox3.Value) * Len(.TextBox1.Value) * Len(.ComboBox7.Value) * Len(.ComboBox3.Value) * Len(.TextBox4.Value) * Len(.TextBox5.Value) * Len(.ComboBox4.Value) * Len(.ComboBox6.Value) * Len(.ComboBox5.Value) * Len(.TextBox7.Value) * Len(.TextBox8.Value) * Len(.TextBox6.Value) * Len(.ComboBox2.Value) * Len(.TextBox9.Value) = 0 Then
        MsgBox "Please Complete All Fields Before Submit"
    Else
        If TextBox8.Value > 3 Then
            If MsgBox("TextBox8 > 3" & vbCrLf & "Continue?", vbYesNo) = vbNo Then
                MsgBox "Please change the value of TextBox8"
                TextBox8.SetFocus
            Else             
                eRow = Sheet4.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
                Cells(eRow, 2).Value = ComboBox1.Text
                Cells(eRow, 5).Value = TextBox1.Text
                Cells(eRow, 1).Value = ComboBox7.Text
                Cells(eRow, 6).Value = ComboBox3.Text
                Cells(eRow, 15).Value = ComboBox2.Text
                Cells(eRow, 17).Value = TextBox2.Text
                Cells(eRow, 18).Value = TextBox3.Text
                Cells(eRow, 9).Value = ComboBox4.Text
                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 If
    End If
End With
End Sub

-----------编辑----------- ----------------------

Private Sub CommandButton1_Click()

Sheets("Overall").Activate

Dim m As Variant, RequiredRange As Variant
Dim msg As Integer

    RequiredRange1 = Array("30S", "30A", "40S")
    RequiredRange2 = Array("10A", "15S", "15A", "20S")
    RequiredRange3 = Array("30S", "30A", "40S")

If Me.ComboBox2.Value = "NiPd" Then
m = Application.Match(ComboBox6.Value, RequiredRange1, False)
    If IsError(m) Then

    msg = MsgBox("Stabilizer Reading:" & ComboBox6.Value & Chr(10) & _
     "Selection Value Out Of Range" & Chr(10) & Chr(10) & _
     "Do You Want To Continue With Submission?", 36, "Warning")

    If msg = 7 Then Me.ComboBox6.SetFocus: Exit Sub
    End If
    End If


   If Me.ComboBox2.Value = "NiAu" Then

    m = Application.Match(ComboBox6.Value, RequiredRange2, False)
    If IsError(m) Then

    msg = MsgBox("Stabilizer Reading:" & ComboBox6.Value & Chr(10) & _
     "Selection Value Out Of Range" & Chr(10) & Chr(10) & _
     "Do You Want To Continue With Submission?", 36, "Warning")

    If msg = 7 Then Me.ComboBox6.SetFocus: Exit Sub
    End If
    End If

    If Me.ComboBox2.Value = "NiPdAu" Then

    m = Application.Match(ComboBox6.Value, RequiredRange3, False)
    If IsError(m) Then

    msg = MsgBox("Stabilizer Reading:" & ComboBox6.Value & Chr(10) & _
     "Selection Value Out Of Range" & Chr(10) & Chr(10) & _
     "Do You Want To Continue With Submission?", 36, "Warning")

    If msg = 7 Then Me.ComboBox6.SetFocus: Exit Sub
    End If
    End If

With Me
    If Len(.ComboBox1.Value) * Len(.TextBox1.Value) * Len(.ComboBox7.Value) * Len(.ComboBox3.Value) * Len(.ComboBox2.Value) * Len(.TextBox2.Value) * Len(.TextBox3.Value) * Len(.ComboBox4.Value) * Len(.ComboBox5.Value) * Len(.TextBox4.Value) * Len(.TextBox5.Value) * Len(.TextBox6.Value) * Len(.ComboBox6.Value) * Len(.TextBox7.Value) * Len(.TextBox8.Value) * Len(.TextBox9.Value) = 0 Then
        MsgBox "Please Complete All Fields Before Submit"
    Else

        If CSng(.TextBox8.Text) > 3 Then
            If MsgBox("Plating Rate below than 3.0 um, Kindly stop production and use another Ni Bath" & vbLf & vbLf & _
                      "Do you wish to continue?", vbYesNo, "Exceeds") = vbNo Then

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

        If CSng(.TextBox8.Text) = 3.2 Then
            If MsgBox("Plating Rate below than 3.2 um , Standby the next Ni bath and start heat up to 65°" & vbLf & vbLf & _
                      "Do you wish to continue?", vbYesNo, "Exceeds") = vbNo Then

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


                Exit Sub
             End If
                      End If
         End If

        eRow = Sheet4.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
        Cells(eRow, 2).Value = ComboBox1.Text
        Cells(eRow, 5).Value = TextBox1.Text
        Cells(eRow, 1).Value = ComboBox7.Text
        Cells(eRow, 6).Value = ComboBox3.Text
        Cells(eRow, 15).Value = ComboBox2.Text
        Cells(eRow, 17).Value = TextBox2.Text
        Cells(eRow, 18).Value = TextBox3.Text
        Cells(eRow, 9).Value = ComboBox4.Text
        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 If
End With
End Sub

【问题讨论】:

  • 在您的代码中,您所谈论的内容没有任何内容。您是否要保留已在代码中的检查:If TextBox8.Value > 3?所以你想用额外的检查来补充这个检查吗?

标签: vba excel


【解决方案1】:

养成使用 tab 和 shift+tab 适当增加代码的习惯,以获得更好的可读性

这应该对你有帮助:

Private Sub CommandButton1_Click()

Sheets("Overall").Activate

Dim m As Variant, RequiredRange As Variant
Dim msg As Integer

    RequiredRange1 = Array("30S", "30A", "40S")
    RequiredRange2 = Array("10A", "15S", "15A", "20S")
    RequiredRange3 = Array("30S", "30A", "40S")

If Me.ComboBox2.Value = "NiPd" Then
    m = Application.Match(ComboBox6.Value, RequiredRange1, False)
    If IsError(m) Then
        msg = MsgBox("Stabilizer Reading:" & ComboBox6.Value & Chr(10) & _
            "Selection Value Out Of Range" & Chr(10) & Chr(10) & _
            "Do You Want To Continue With Submission?", 36, "Warning")
        If msg = 7 Then Me.ComboBox6.SetFocus
        Exit Sub
    End If
End If


If Me.ComboBox2.Value = "NiAu" Then
    m = Application.Match(ComboBox6.Value, RequiredRange2, False)
    If IsError(m) Then
        msg = MsgBox("Stabilizer Reading:" & ComboBox6.Value & Chr(10) & _
            "Selection Value Out Of Range" & Chr(10) & Chr(10) & _
            "Do You Want To Continue With Submission?", 36, "Warning")
        If msg = 7 Then Me.ComboBox6.SetFocus
        Exit Sub
    End If
End If

If Me.ComboBox2.Value = "NiPdAu" Then
    m = Application.Match(ComboBox6.Value, RequiredRange3, False)
    If IsError(m) Then
        msg = MsgBox("Stabilizer Reading:" & ComboBox6.Value & Chr(10) & _
             "Selection Value Out Of Range" & Chr(10) & Chr(10) & _
             "Do You Want To Continue With Submission?", 36, "Warning")
        If msg = 7 Then Me.ComboBox6.SetFocus
        Exit Sub
    End If
End If

With Me
    If Len(.ComboBox1.Value) * Len(.TextBox1.Value) * Len(.ComboBox7.Value) * Len(.ComboBox3.Value) * Len(.ComboBox2.Value) * Len(.TextBox2.Value) * Len(.TextBox3.Value) * Len(.ComboBox4.Value) * Len(.ComboBox5.Value) * Len(.TextBox4.Value) * Len(.TextBox5.Value) * Len(.TextBox6.Value) * Len(.ComboBox6.Value) * Len(.TextBox7.Value) * Len(.TextBox8.Value) * Len(.TextBox9.Value) = 0 Then
        MsgBox "Please Complete All Fields Before Submit"
    Else
        If CSng(.TextBox8.Text) > 3 Then
            If MsgBox("Plating Rate below than 3.0 um, Kindly stop production and use another Ni Bath" & vbLf & vbLf & _
                      "Do you wish to continue?", vbYesNo, "Exceeds") = vbNo Then
                    MsgBox "user to re-type the value in TextBox8.", vbInformation, "Warning"
                If CSng(.TextBox8.Text) = 3.2 Then
                    If MsgBox("Plating Rate below than 3.2 um , Standby the next Ni bath and start heat up to 65°" & vbLf & vbLf & _
                              "Do you wish to continue?", vbYesNo, "Exceeds") = vbNo Then
                        MsgBox "user to re-type the value in TextBox8.", vbInformation, "Warning"
                        Exit Sub
                    End If
                End If
            End If
            eRow = Sheet4.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
            Cells(eRow, 2).Value = ComboBox1.Text
            Cells(eRow, 5).Value = TextBox1.Text
            Cells(eRow, 1).Value = ComboBox7.Text
            Cells(eRow, 6).Value = ComboBox3.Text
            Cells(eRow, 15).Value = ComboBox2.Text
            Cells(eRow, 17).Value = TextBox2.Text
            Cells(eRow, 18).Value = TextBox3.Text
            Cells(eRow, 9).Value = ComboBox4.Text
            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
    Else
        eRow = Sheet4.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
        Cells(eRow, 2).Value = ComboBox1.Text
        Cells(eRow, 5).Value = TextBox1.Text
        Cells(eRow, 1).Value = ComboBox7.Text
        Cells(eRow, 6).Value = ComboBox3.Text
        Cells(eRow, 15).Value = ComboBox2.Text
        Cells(eRow, 17).Value = TextBox2.Text
        Cells(eRow, 18).Value = TextBox3.Text
        Cells(eRow, 9).Value = ComboBox4.Text
        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

【讨论】:

  • 您好,我已经修改了代码,但是如果textbox8中的值小于3,则不会将数据存储在excel表中。请检查我已修改的代码的问题。
猜你喜欢
  • 2014-07-19
  • 1970-01-01
  • 1970-01-01
  • 2014-02-18
  • 1970-01-01
  • 2016-02-23
  • 2014-07-17
  • 1970-01-01
  • 2013-05-05
相关资源
最近更新 更多