Question

I have a columns of checkboxes in Excel. When one of them is checked, I want to prompt the user "Are you sure?" or something to that effect. If they answer "Yes", move on and do nothing. If they answer no, I want to uncheck the box (the one they just clicked). It seems simple enough, but I can't for some reason get it to work.

I've tried something like:

r = MsgBox("Are you sure", vbYesNo, "Eh?")
If r <> vbYes Then Application.Undo

But that doesn't quite work. It seems so simple, yet it ends up being a pain.

Was it helpful?

Solution

The problem is that checking a checkbox doesn't trigger an Excel action that can be undone. (Doesn't matter if the controls are Form or ActiveX.)

If your checkboxes are ActiveX controls then you can do this:

Private Sub VerifyCheckBox(chk As CheckBox)
   If chk.Value Then ' only ask question if checkbox has been checked
      If MsgBox("Are you sure", vbYesNo, "Eh?") <> vbYes Then
         chk.Value = Not chk.Value
      End If
   End If
End Sub

You would then call that in each checkbox's click event.

Private Sub CheckBox1_Click()
   VerifyCheckBox CheckBox1
End Sub

Private Sub CheckBox2_Click()
   VerifyCheckBox CheckBox2
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top