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.
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.
Try this
Copy the codeCode: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
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
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.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?
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.
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.
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
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.
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
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
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"?
Thank you, SHG... this one works great too! I apreciate it!
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
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
Thank you so much to everyone! You, guys, are great!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks