簡體   English   中英

僅當用戶修改現有值時,才會彈出禁用消息框

[英]Disable message box pop up only when user amending the existing value

如果輸入的值為零,我有vba代碼彈出msg框。 如果用戶以后想要將零更改為其他某個數字,它仍然會彈出msg框,然后才允許將零編輯為其他數字。

如果用戶正在更改現有值,我不希望彈出msgbox

Private Sub Worksheet_Change(ByVal Target As Range)

    Application.EnableEvents = False

    If Intersect(Target, Range("b4:af18")) Is Nothing Then

        If Target.Value = 0 Then
            Application.EnableEvents = True
            MsgBox "This is it", vbApplicationModal, "Scikess/Holiday"
        End If 

    End If

    Application.EnableEvents = True

End Sub

我相信您正在描述用戶在輸入新答案之前點擊刪除鍵的情況。 在這種情況下,空白單元格的數值為零,並且您在檢查中得到誤報以查看用戶是否鍵入零。

Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Intersect(Target, Range("b4:af18")) Is Nothing Then
        On Error GoTo bm_Safe_Exit
        Dim trgt As Range
        Application.EnableEvents = False
        For Each trgt In Intersect(Target, Range("b4:af18"))
            If trgt.Value = 0 And trgt.Text = "0" Then
                MsgBox "This is it" & Chr(10) & trgt.Address(0, 0) & " cannot be zero.", _
                    vbApplicationModal, "Scikess/Holiday"
                Application.Undo
                trgt.Activate
                Exit For
            End If
        Next trgt

    End If

bm_Safe_Exit:
    Application.EnableEvents = True

End Sub

我試圖在涉及多個Target時運行代碼(例如粘貼或填充操作),但這會使Application.Undo難以保留其他有效條目。 如果不適合,請對此行為發表評論。

它還沒有明確你希望這個反應的范圍。 您似乎錯過了Intersect方法中Not ,使其對任何不在范圍內的單元格做出反應。 我已在上面調整以包括您指定的范圍,而不是將其排除。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM