Prompt messagebox depending on cell value

CV899000

Board Regular
Joined
Feb 11, 2016
Messages
98
Hi,

I need to have a messagebox prompt depending on a cell value.

If cell A20 is NOT "Yes" and the user chooses "Yes" in cell A22, then a message box should prompt saying "This option is only possible by enabling A20" and then if the user presses "Ok" in the message box, then cell A20 should change to "Yes" and cell B20 should change to "1".

If the user chooses "No" in the message box, the A22 cell shall be cleared (empty).



If cell A20 contains "Yes" before choosing anything in A22, then the message box shall not appear.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Try this:-

Code:
If Cells(22,1)="" then Exit Sub      
If Cells(20, 1).Value <> "Yes" And Cells(22, 1).Value = "Yes" Then
    FixIt = MsgBox("This option is only possible by enabling A20" & vbCrLf & vbCrLf & "Fix it?", vbYesNo)
    If FixIt = vbYes Then 
        Cells(20, 1).Value = "Yes"
        Cells(20, 2).Value = 1
    Else
        Cells(22,1).value = ""
End If

You can tell the msgbox to be a Yes/No type rather than a standard OK, and it will return either vbYes or vbNo values depending on what was clicked.

You need the worksheet_Change sub name as per CV's post below. You need the first line because if the code changes the value in A22 then the code will run again, if you didn't have that it might go into an endless loop.
 
Last edited:
Upvote 0
I have this code for now, but this does not work perfect.
Code:
[Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("A22").Address Then
If Target.Value = "Yes" Then
If MsgBox("A20 is required for this option. Enable?", vbYesNo) = vbNo Then
Worksheets("Choose sheet").Range("A22").Value = ""
Exit Sub
End If
Worksheets("Choose sheet").Range("A20").Value = "Yes"
Worksheets("Choose sheet").Range("B20").Value = "1"
Exit Sub
End If
End If
End Sub/CODE]

If cell A20 is "yes", and the user chooses "yes" in cell A22, then the message box still prompts. I need something that checks if cell A20 is "Yes" and if so, then stops the sub before it prompts the msgbox.
 
Upvote 0
This works for me. I missed out an End I I think.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address <> Range("A22").Address Then Exit Sub
If Target.Value = "" Then Exit Sub
If Target.Offset(-2, 0).Value <> "Yes" And Target.Value = "Yes" Then
    FixIt = MsgBox("This option is only possible by enabling A20" & vbCrLf & vbCrLf & "Fix it?", vbYesNo)
    If FixIt = vbYes Then
        Target.Offset(-2, 0).Value = "Yes"
        Target.Offset(0, 1).Value = 1
    Else
        Target.Value = ""
    End If
End If

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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