VBA - Seek user confirmation when changing non blank cells

TORPIDO

New Member
Joined
Aug 2, 2017
Messages
26
hi yall

i have below code which mostly works except it also asks confirmation for empty cells, i want it to ignore the rule for blank cells where value are being set for first time, is this doable?

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim oldValue As Variant
    Dim newValue As Variant
    Dim userResponse As Integer

    If Target.Count > 1 Then Exit Sub

    Application.EnableEvents = False

    If Not IsEmpty(Target) Then
        newValue = Target.Value
        Application.Undo
        oldValue = Target.Value

        If oldValue <> newValue Then
            userResponse = MsgBox("Do you want to change the value?", vbYesNo + vbQuestion, "Confirm Change")

            If userResponse = vbNo Then
                Target.Value = oldValue
            Else
                Target.Value = newValue
            End If
        End If
    End If

    Application.EnableEvents = True
End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
try


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim oldValue As Variant
    Dim newValue As Variant
    Dim userResponse As Integer

    If Target.Count > 1 Then Exit Sub

    Application.EnableEvents = False

    If Not IsEmpty(Target) Then
        newValue = Target.Value
        Application.Undo
        oldValue = Target.Value

        If oldValue <> newValue And IsEmpty(Target.Value) = False Then
            userResponse = MsgBox("Do you want to change the value?", vbYesNo + vbQuestion, "Confirm Change")

            If userResponse = vbNo Then
                Target.Value = oldValue
            Else
                Target.Value = newValue
            End If
        End If
    End If

    Application.EnableEvents = True
End Sub
 
Upvote 0
try


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim oldValue As Variant
    Dim newValue As Variant
    Dim userResponse As Integer

    If Target.Count > 1 Then Exit Sub

    Application.EnableEvents = False

    If Not IsEmpty(Target) Then
        newValue = Target.Value
        Application.Undo
        oldValue = Target.Value

        If oldValue <> newValue And IsEmpty(Target.Value) = False Then
            userResponse = MsgBox("Do you want to change the value?", vbYesNo + vbQuestion, "Confirm Change")

            If userResponse = vbNo Then
                Target.Value = oldValue
            Else
                Target.Value = newValue
            End If
        End If
    End If

    Application.EnableEvents = True
End Sub
Now its not applying any changes on the blank cell, no confirmation is asks, just rolls back the value to blank
 
Upvote 0
OK, Try this

VBA Code:
Dim OldValue As Variant

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim newValue As Variant
    Dim userResponse As Integer

    If Target.Count > 1 Then Exit Sub

    Application.EnableEvents = False
    If Not IsEmpty(OldValue) Then
        newValue = Target.Value

        If OldValue <> newValue Then
            userResponse = MsgBox("Do you want to change the value?", vbYesNo + vbQuestion, "Confirm Change")

            If userResponse = vbNo Then
                Target.Value = OldValue
            Else
                Target.Value = newValue
            End If
        End If
    End If

    Application.EnableEvents = True
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
   OldValue = Target.Value
End Sub
 
Upvote 0
Solution
OK, Try this

VBA Code:
Dim OldValue As Variant

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim newValue As Variant
    Dim userResponse As Integer

    If Target.Count > 1 Then Exit Sub

    Application.EnableEvents = False
    If Not IsEmpty(OldValue) Then
        newValue = Target.Value

        If OldValue <> newValue Then
            userResponse = MsgBox("Do you want to change the value?", vbYesNo + vbQuestion, "Confirm Change")

            If userResponse = vbNo Then
                Target.Value = OldValue
            Else
                Target.Value = newValue
            End If
        End If
    End If

    Application.EnableEvents = True
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
   OldValue = Target.Value
End Sub
fantastic, this i why i say AI cannot remove humans... i tried 3 AI (Bard, GPT, Bing) non could figure it out
thanks a lot
 
Upvote 0
Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim oldValue As Variant
    Dim newValue As Variant
    Dim userResponse As Integer
    
    If Target.Count > 1 Then Exit Sub
    Application.EnableEvents = False
    If Not IsEmpty(Target) Then
        newValue = Target.Value
        Application.Undo
        oldValue = Target.Value
        If oldValue <> newValue And oldValue <> "" Then
            userResponse = MsgBox("Do you want to change the value?", vbYesNo + vbQuestion, "Confirm Change")
            If userResponse = vbNo Then
                Target.Value = oldValue
            Else
                Target.Value = newValue
            End If
            Else
            Target.Value = newValue
        End If
    End If
    Application.EnableEvents = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top