Add "Are you sure?" to my excel button, how can I? - vba

I have a button on my form that clears the entire 8 sheet workbook. I do want to clear it occasionally, but I would hate to do it by accident. I've tried googling it, but every result I've found assumes I have a much firmer grasp of VBA than I do. How can I make it so when the button is clicked a Dialog box pops up saying "This will erase everything! Are you sure? [Continue] [Cancel]"?
Thanks.

On your existing button code, simply insert this line before the procedure:
If MsgBox("This will erase everything! Are you sure?", vbYesNo) = vbNo Then Exit Sub
This will force it to quit if the user presses no.

Create a new sub with the following code and assign it to your button. Change the "DeleteProcess" to the name of your code to do the deletion. This will pop up a box with OK or Cancel and will call your delete sub if you hit ok and not if you hit cancel.
Sub AreYouSure()
Dim Sure As Integer
Sure = MsgBox("Are you sure?", vbOKCancel)
If Sure = 1 Then Call DeleteProcess
End Sub
Jesse

Just make a custom userform that is shown when the "delete" button is pressed, then link the continue button to the actual code that does the deleting. Make the cancel button hide the userform.

Related

Excel VBA Custom dialog box cancel button code

I have a dialog box pop up with radio buttons, the radio buttons currently run macros. However, once I have the dialog box pop I can't close it. I have added a cancel button. The code is below
Private Sub CommandButton1_Click()
Cmd_Cancel
End Sub
How can I get my dialog box to close by pressing the cancel button?
In my form I used a Cancel button with the below code for it
btnCancel is the (Name) of the button
Private Sub btnCancel_Click()
Unload Form_Form1
End Sub
Depends on what you want. The keyword "Me" is the actual instance of the Form-Object. Hide it and its still in the memory. Or unload it to kill it:
When it's only hidden you can show it again with .show (from another piece of code...)
Private Sub CommandButton1_Click()
Me.Hide
Unload Me
End Sub

VBA Button to Clear a Range of Cells

As the title states, I am trying to create a button to clear a range of cells. Prior to clearing the cells, I have a dialog box pop up to confirm the selection. Here is my code:
Private Sub CommandButton1_Click()
If MsgBox("THIS WILL CLEAR EVERYTHING IN THE CART! Are you sure?", vbYesNo) = vbNo Then Exit Sub
Range("A28:AA47").ClearContents
End Sub
The code works when I'm in Design Mode and press "Play" but the button won't work on my sheet when I'm out of Design Mode.
Is the code completely ending itself when I select "No", to the point where it will not run again even if I hit the button again?
Your code looks a little incomplete. Try this instead:
EDIT: Cleaned it up a little more. No need for a vbNo, as if you don't select yes, the subroutine will simply end.
Private Sub CommandButton1_Click()
If MsgBox("THIS WILL CLEAR EVERYTHING IN THE CART! Are you sure?", vbYesNo) = vbYes Then
Range("A28:AA47").ClearContents
End If
End Sub

vba userform checking the GroupName instead of an individual button

I'm using a VBA Userform. The MassPrompt userform has a set of six GroupNames and some text boxes. Each GroupName contains two or more radio buttons.
I'd like the following code to be triggered anytime any element within the GroupName "GROnly" changes. If the user made an inappropriate button choice in "GROnly" based on the choice in another group, I'd like to display a warning message and present the MassPrompt userform again.
Right now, I've assigned the code to the one button "GROnly_yes". It works, but only when that one button is clicked. How do I position this within the UserForm code to trigger anytime a button with the GroupName "GROnly" is clicked? Thanks for looking at this.
Private Sub GROnly_yes_Click()
'Prompt if the GROnly is true and it's inappropriate for the GSetting choice
If GROnly_yes = True And GSetting_renewal = True _
Then
GROnly_yes = False
GROnly_no = True
MsgBox ("The GROnly can't be chosen with a Renewal." & vbNewLine & _
"The GROnly button has been changed to 'NO'.")
UserForm_Initialize
End If
'Other IF statements here.
End Sub
If I understood well, GRonly is the GroupBox that contains (let's say) the radio_button_1 and radio_button_2.
The reason why the code doesn't trigger is that when he/she changes the value of one radio-button is not clicking on the GroupBox, but rather changing the value of that single radio-button.
You will have to add the code to the _Change event of the radio button objects. This is an example:
Sub myFunctionalCode()
'your code here
End Sub
Private Sub radio_button_1_Change()
myFunctionalCode
End Sub
Private Sub radio_button_2_Change()
myFunctionalCode
End Sub
Thanks for the reply. That would work.
In the meantime, I came up with another alternative as well. I removed the code from:
Private Sub GROnly_yes_Click()
And I moved it to the button that the user clicks to submit the form:
Private Sub ContinueButton_Click()
In my original code I had to change UserForm_Initialize to Exit Sub to make the code work in the "submit" button.
Thanks again for your help.
Phil

Using a VBA Dialog Box when closing a SpreadSheet

I would like a dialog box to appear when someone attempts to close an important Excel sheet. Namely I would like it to ask "Have you logged all Changes?" giving them the opportunity to click "Yes" or "No". If they click "Yes" the sheet will close, but if they Click "No" it will ask them to "Please log all changes in the Change Notes section".
Any and all help greatly appreciated.
This is the code you are looking for. It asks the question, and if No is clicked, it cancels the closing of the workbook:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ureply = MsgBox("Have you Saved All Changes?", vbYesNo)
If ureply = vbNo Then
Cancel = True
End If
End Sub
This code needs to go on 'ThisWorkbook' (Shown above the word 'Modules') on your VBA editor

VBA - leave current cell

I have some code that reacts to a double click. This action works just fine, my question though.. after filling in the form that pops up due to the double click action the form is unloaded, no problem here, but the user is left in edit mode inside the cell he double clicked on.
Now normally in Excel you can press CTRL-Enter and the cell will just simply be selected and you out of edit mode. How do I achieve this in VBA ?
All I can seem to work is moving the cell up one, but really just want the user to exit Edit mode and stay on the current cell, the one he double clicked on.
Private Sub Cancel_Click()
Cells(ActiveCell.Column, ActiveCell.Row - 1).Activate
Cells(ActiveCell.Column, ActiveCell.Row).Activate
Unload Term
End Sub
Any suggestions ?
Use the BeforeDoubleClick's Cancel argument, which ... cancels clicking into the cell:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
UserForm1.Show
End Sub