【问题标题】:Can I get VBA to show a confirmation msgbox when a cell is changed, that "undoes" the change if the user selects "no"我可以让 VBA 在更改单元格时显示确认消息框,如果用户选择“否”,则“撤消”更改
【发布时间】:2022-11-03 18:12:41
【问题描述】:

我正在使用 Excel 为我的工作学时创建一个跟踪器,日期在 D 行水平运行,E 行和 F 行分别跟踪获得的小时数和花费的小时数,分别低于每个日期。

我已经有代码检查用户是否选择了一个超过当前日期的单元格,这会引发一个确认消息框,上面写着“这个日期是在未来,你确定要继续吗?”

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim myDate As Variant, rng As Range

    On Error GoTo ws_exit
    Application.EnableEvents = False

    If Not Intersect(Target, Me.Range("C5:CZO5")) Is Nothing And ActiveCell.Offset(-1, 0) > Date Then
        With Target
            If MsgBox("Caution: You are about to modify a future date, are you sure you want to do this?", vbExclamation + vbYesNo + vbDefaultButton1, "Modification Warning") = vbNo Then
                Set rng = Range("C4:CZO4")
                Set myDate = rng.Find(What:=Int(Date), LookIn:=xlValues)
                Cells(myDate.Row, myDate.Column).Select
                Set rng = Nothing
            End If
        End With

    ElseIf Not Intersect(Target, Me.Range("C6:CZO6")) Is Nothing And ActiveCell.Offset(-2, 0) > Date Then
        With Target
            If MsgBox("Caution: You are about to modify a future date, are you sure you want to do this?", vbExclamation + vbYesNo + vbDefaultButton1, "Modification Warning") = vbNo Then
                Set rng = Range("C4:CZO4")
                Set myDate = rng.Find(What:=Int(Date), LookIn:=xlValues)
                Cells(myDate.Row, myDate.Column).Select
                Set rng = Nothing
            End If
        End With
    End If

ws_exit:
    Application.EnableEvents = True
End Sub

这很好用,尽管我现在想创建一个单独的 msgbox,它会要求用户在编辑单元格的值后确认他们是否获得了直线经理的授权。如果用户选择“否”,我希望代码撤消最后一次更改(即更改单元格的值)。 这可能吗? 到目前为止,我的代码会调出 msgbox,但按“否”不会撤消值更改。

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("C5:CZO6")) Is Nothing Then
        With Target
            If MsgBox("Caution: Ensure line manager's permission is granted, do you want to continue?", vbCaution + vbYesNo + vbDefaultButton1, "Authourisation Reminder") = vbNo Then
                Set rng = Range("C4:CZO4")
                Set myDate = rng.Find(What:=Int(Date), LookIn:=xlValues)
                Cells(myDate.Row, myDate.Column).Select
                Set rng = Nothing
            End If
        End With

    End If
End Sub

任何帮助都会像往常一样受到赞赏! :)

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    使用应用程序。撤消

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Range("C5:CZO6")) Is Nothing Then
            With Target
                If MsgBox("Caution: Ensure line manager's permission is granted, do you want to continue?", vbCaution + vbYesNo + vbDefaultButton1, "Authourisation Reminder") = vbNo Then
    Application.Undo ' this should do the trick
                    Set rng = Range("C4:CZO4")
                    Set myDate = rng.Find(What:=Int(Date), LookIn:=xlValues)
                    Cells(myDate.Row, myDate.Column).Select
                    Set rng = Nothing
                End If
            End With
    
        End If
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-07
      • 1970-01-01
      • 2018-08-12
      • 1970-01-01
      • 2013-12-09
      • 1970-01-01
      相关资源
      最近更新 更多