+ Reply to Thread
Results 1 to 15 of 15
  1. #1
    Registered User
    Join Date
    10-15-2009
    Location
    Kirkland, WA - USA
    MS-Off Ver
    Excel 2007
    Posts
    16

    Confirmation pop-up on cell value change

    Hello, all! I need help with getting a confirmation pop-up message with Yes and No buttons that confirms user's intention to change cell's value on the sheet. In my project, this pop-up message should only work in columns D, E, and F. The reason for the pop-up is to prevent accidental change of data in these columns.
    Last edited by chrisneu; 10-15-2009 at 02:58 PM.

  2. #2
    Forum Guru Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007
    Posts
    3,441

    Re: Confirmation pop-up on cell value change

    I think is is going to get pretty annoying for the user, but here is one way to go about it - modify as required.

    The code needs to go into the sheet module.

    Code:
    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
        Dim response As Variant, msg As String
        
        msg = "You have changed the data - please confirm this change is intended"
        
        If Target.Count > 1 Then Exit Sub
        
        If Not Intersect(Target, Columns("D:F")) Is Nothing Then
            response = MsgBox(msg, vbYesNo + vbExclamation)
            Select Case response
                Case vbYes
                    Exit Sub
                    
                Case vbNo
                    MsgBox ("Please correct the entry as required")
            End Select
        End If
        
    End Sub
    Palmetto

    Do you know . . . ?

    You can leave feedback and add to the reputation of all who contributed a helpful response to your solution by clicking the star icon located at the left in one of their post in this thread.

  3. #3
    Forums Administrator royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    24,447

    Re: Confirmation pop-up on cell value change

    Try this
    Code:
    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim OldValue As Variant
        If Not Intersect(Target, Range(Cells(1, 4), Cells(Rows.Count, 6))) Is Nothing Then
            Application.EnableEvents = False
            OldValue = Target.Value
            Select Case MsgBox("Are you sure that you want to change "" & Target.Address", _
                               vbYesNo Or vbQuestion Or vbDefaultButton1, "Confirm change...")
                Case vbYes
                    Exit Sub
                Case vbNo
                    Target.Value = OldValue
            End Select
        End If
        Application.EnableEvents = True
    End Sub
    Copy the code
    Select the worksheet in which you the code to run
    Right click on the sheet tab and choose View Code, to open the Visual Basic Editor.
    Where the cursor is flashing, choose Edit | Paste
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel consulting, free examples and tutorials visit Excel Consulting-Excel VBA
    Check out the free Excel Toolbar

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)


    Code Tags: Make your code easier for us to read

  4. #4
    Registered User
    Join Date
    10-15-2009
    Location
    Kirkland, WA - USA
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Confirmation pop-up on cell value change

    Thank you very much, Palmetto. This is very close to what I want, but I do not want this message to pop up if I just double-clicked the cell but made no changes and if the change was made and I click "No" to accept the change, your code generates a message advising to correct the entry as required. Is it possible that it just puts back the old value?

  5. #5
    Forum Guru Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007
    Posts
    3,441

    Re: Confirmation pop-up on cell value change

    Thank you very much, Palmetto. This is very close to what I want, but I do not want this message to pop up if I just double-clicked the cell but made no changes and if the change was made and I click "No" to accept the change, your code generates a message advising to correct the entry as required. Is it possible that it just puts back the old value?
    The code is RoyUK's post is closer to what you want and should do the job. Give it a try and if need further refinement, post back.
    Palmetto

    Do you know . . . ?

    You can leave feedback and add to the reputation of all who contributed a helpful response to your solution by clicking the star icon located at the left in one of their post in this thread.

  6. #6
    Registered User
    Join Date
    10-15-2009
    Location
    Kirkland, WA - USA
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Confirmation pop-up on cell value change

    Thank you very much, royUK. It worked with the first-cell try, but then it stopped working for some reason. I saved the file, re-pasted the code, re-opened the file... it still does not work. The sheet and cells act just like they would without the code.

  7. #7
    Forums Administrator royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    24,447

    Re: Confirmation pop-up on cell value change

    Fixed it
    Code:
    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
       Dim OldValue As Variant
        If Not Intersect(Target, Columns("D:F")) Is Nothing Then
            Application.EnableEvents = False
            OldValue = Target.Value
            Select Case MsgBox("Are you sure that you want to change " & Target.Address, _
                               vbYesNo Or vbQuestion Or vbDefaultButton1, "Confirm change...")
                Case vbYes
                    GoTo exit_handler
                Case vbNo
                    Target.Value = OldValue
            End Select
        End If
    exit_handler:
        Application.EnableEvents = True
    End Sub
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel consulting, free examples and tutorials visit Excel Consulting-Excel VBA
    Check out the free Excel Toolbar

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)


    Code Tags: Make your code easier for us to read

  8. #8
    Registered User
    Join Date
    10-15-2009
    Location
    Kirkland, WA - USA
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Confirmation pop-up on cell value change

    Thank you very much, RoyUK. The pop-up works now, but it appears that buttons Yes and No have the same function, which is to accept the change.

    Also, if I just double-click a cell but then leave the cell withouth making any changes, I don't want the message to pop-up.

  9. #9
    Forums Administrator royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    24,447

    Re: Confirmation pop-up on cell value change

    This should work
    Code:
    Option Explicit
       Dim OldValue As Variant
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Columns("D:F")) Is Nothing Then
            Application.EnableEvents = False
            Select Case MsgBox("Are you sure that you want to change " & Target.Address, _
                               vbYesNo Or vbQuestion Or vbDefaultButton1, "Confirm change...")
                Case vbYes
                    GoTo exit_handler
                Case vbNo
                    Target.Value = OldValue
            End Select
        End If
    exit_handler:
        Application.EnableEvents = True
    End Sub
    
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        OldValue = Target.Value
    End Sub
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel consulting, free examples and tutorials visit Excel Consulting-Excel VBA
    Check out the free Excel Toolbar

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)


    Code Tags: Make your code easier for us to read

  10. #10
    Forum Moderator shg's Avatar
    Join Date
    06-21-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007
    Posts
    25,137

    Re: Confirmation pop-up on cell value change

    Here's another way ...
    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim vNew        As Variant
        Dim vOld        As Variant
    
        With Target
            If .Count > 1 Or Intersect(.Cells, Columns("D:F")) Is Nothing Then Exit Sub
            vNew = .Value
            Application.EnableEvents = False
            Application.Undo
            vOld = .Value
            .Value = vNew
            If MsgBox(Prompt:="Change value??", _
                      Buttons:=vbYesNo + vbQuestion + vbDefaultButton2, _
                      Title:="Are you sure??") = vbNo Then
                .Value = vOld
            End If
            Application.EnableEvents = True
        End With
    End Sub
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  11. #11
    Registered User
    Join Date
    10-15-2009
    Location
    Kirkland, WA - USA
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Confirmation pop-up on cell value change

    Thanks a lot, RoyUK! That is exactly what I wanted and it works great!

    One last question: when the pop-up message refers to the cell address, like $E$4, is going to be too much effort to eliminate the "$" character from that message, leaving only "E4"?

  12. #12
    Registered User
    Join Date
    10-15-2009
    Location
    Kirkland, WA - USA
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Confirmation pop-up on cell value change

    Thank you, SHG... this one works great too! I apreciate it!

  13. #13
    Forums Administrator royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    24,447

    Re: Confirmation pop-up on cell value change

    remove the $ with subtitut
    Code:
    Option Explicit
    Dim OldValue   As Variant
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim sAddress As String
        If Not Intersect(Target, Columns("D:F")) Is Nothing Then
            sAddress = Target.Address
            sAddress = WorksheetFunction.Substitute(sAddress, "$", "")
    
            Application.EnableEvents = False
            Select Case MsgBox("Are you sure that you want to change " & sAddress, _
                               vbYesNo Or vbQuestion Or vbDefaultButton1, "Confirm change...")
                Case vbYes
                    GoTo exit_handler
                Case vbNo
                    Target.Value = OldValue
            End Select
        End If
    exit_handler:
        Application.EnableEvents = True
    End Sub
    
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        OldValue = Target.Value
    End Sub
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel consulting, free examples and tutorials visit Excel Consulting-Excel VBA
    Check out the free Excel Toolbar

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)


    Code Tags: Make your code easier for us to read

  14. #14
    Forums Administrator royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    24,447

    Re: Confirmation pop-up on cell value change

    Or

    Code:
    Option Explicit
    Dim OldValue   As Variant
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Columns("D:F")) Is Nothing Then
            Application.EnableEvents = False
            Select Case MsgBox("Are you sure that you want to change " & sAddress(Target), _
                               vbYesNo Or vbQuestion Or vbDefaultButton1, "Confirm change...")
                Case vbYes
                    GoTo exit_handler
                Case vbNo
                    Target.Value = OldValue
            End Select
        End If
    exit_handler:
        Application.EnableEvents = True
    End Sub
    
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        OldValue = Target.Value
    End Sub
    
    Function sAddress(TheRange As Range) As String
      sAddress = TheRange.Address
            sAddress = WorksheetFunction.Substitute(sAddress, "$", "")
    End Function
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel consulting, free examples and tutorials visit Excel Consulting-Excel VBA
    Check out the free Excel Toolbar

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)


    Code Tags: Make your code easier for us to read

  15. #15
    Registered User
    Join Date
    10-15-2009
    Location
    Kirkland, WA - USA
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Confirmation pop-up on cell value change

    Thank you so much to everyone! You, guys, are great!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0