【问题标题】:Message box to confirm data change operation确认数据更改操作的消息框
【发布时间】:2015-05-22 10:42:04
【问题描述】:

我的提交按钮代码如下:

    Private Sub CommandButton1_Click()
Sheets("Overall").Activate
With Me
   If Len(.ComboBox1.Value) * Len(.TextBox1.Value) * Len(.ComboBox2.Value) * Len(.ComboBox3.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) = 0 Then
            MsgBox "Please Complete All Fields Before Submit"

        Else

            eRow = Sheet9.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
            Cells(eRow, 1).Value = ComboBox1.Text
            Cells(eRow, 2).Value = TextBox2.Text
            Cells(eRow, 3).Value = TextBox3.Text
            Cells(eRow, 4).Value = TextBox1.Text
            Cells(eRow, 5).Value = ComboBox3.Text
            Cells(eRow, 6).Value = TextBox4.Text
            Cells(eRow, 7).Value = TextBox5.Text
            Cells(eRow, 8).Value = ComboBox4.Text
            Cells(eRow, 15).Value = ComboBox6.Text
            Cells(eRow, 10).Value = ComboBox5.Text
            Cells(eRow, 11).Value = TextBox7.Text
            Cells(eRow, 12).Value = TextBox8.Text
            Cells(eRow, 13).Value = TextBox6.Text
            Cells(eRow, 14).Value = ComboBox2.Text

            End If
    End With
    End Sub

我希望添加一个确认弹出消息框以提醒用户如果在 TextBox8 中键入的值超过 3.0

如果用户选择是,则只将数据存储在excel表中,但如果用户选择否,将显示另一个弹出消息框,通知用户重新输入TextBox8中的值。

我应该在哪里添加 vbYesNo 消息框?

【问题讨论】:

  • 在第一个 Else 之后添加另一个 If 块。此外,您应该对 Cells(eRow, #).Value 做一些事情,因为 Sheet9 可能并不总是处于活动状态。
  • 看这里Link
  • 我的这篇文章有另一种方法,可能至少在理论上有兴趣:yoursumbuddy.com/userform-event-class-validating-controls

标签: vba excel


【解决方案1】:

Cells 更改之前添加类似这样的内容:

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 = ...
    End If
End If

它将显示一个YesNo MsgBox 要求用户继续。如果用户选择No,则会显示第二个带有通知的 MsgBox,然后将激活 TextBox。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-13
    相关资源
    最近更新 更多