VBA to Validate Data

PosieQ

New Member
Joined
Mar 14, 2017
Messages
8
Hello,

I am a true beginner to VBA and would like some help in creating a macro that is triggered when the user goes to save the document. The document contains a check box and, if checked, cell B28 will show TRUE. If the check box is checked, the user is expected to enter additional information in B30. I would like to create a macro with the following conditions so that a message appears when the user tries to save the document:

If B28 is TRUE and B30 is blank, show the message "Please enter a number in B30".

Please let me know if I have not provided enough information. Thank you in advance!
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Copy the code below and paste it into your ThisWorkbook code module. To access the code module, press Alt + F11 amd when the vb editor opens, double click "ThisWorkbook" in the small Project pane at upper left of the vb Editor window. Check the upper margin of the vb Editor window to be sure ThisWorkbook (Code) appears there. Then paste the code into the large code pane. Close the editor and save the workbook as a macro enabled workbook (.xlsm) to preserve the code.

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If ActiveSheet.Range("B28").Value = True And ActiveSheet.Range("B30") = "" Then
    MsgBox "Cell B30 is a required entry when cell B28 is True"
    Cancel = True
End If
End Sub
 
Upvote 0
Thank you, JLGWHiz! This worked perfectly, and I really appreciate your thorough instructions! One question I didn't think of before. After the message pops up that B30 is required, if I wanted to give the user the option of continuing to save without entering anything in B30 (so B3 would not be a required field), how does the code change?
 
Upvote 0
Thank you, JLGWHiz! This worked perfectly, and I really appreciate your thorough instructions! One question I didn't think of before. After the message pops up that B30 is required, if I wanted to give the user the option of continuing to save without entering anything in B30 (so B3 would not be a required field), how does the code change?
Try this
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim ans As Variant
If ActiveSheet.Range("B28").Value = True And ActiveSheet.Range("B30") = "" Then
    ans = MsgBox("Cell B30 is a required entry when cell B28 is True.  Do you want to save the file anyway?", _
        vbQuestion + vbYesNo, "OPTION")
        If ans = vbNo Then
            Cancel = True
        End If
End If
End Sub
 
Upvote 0
Try this
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim ans As Variant
If ActiveSheet.Range("B28").Value = True And ActiveSheet.Range("B30") = "" Then
    ans = MsgBox([COLOR=#FF0000]"Cell B30 is a required entry when cell B28 is True.  Do you want to save the file anyway?"[/COLOR], _
        vbQuestion + vbYesNo, "OPTION")
        If ans = vbNo Then
            Cancel = True
        End If
End If
End Sub


I made a change to the text in the message box (in red font above) to the following but am getting the error message "Compile error: Expected list separator or)". What am I doing wrong?

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim ans As Variant
If ActiveSheet.Range("B28").Value = True And ActiveSheet.Range("B30") = "" Then
ans = MsgBox("A Prime's Prime Number is required when the role 'Subcontractor" is checked. If number was provided, please enter 'TBD' or 'N/A' as appropriate. Do you want to save the file anyway?", _
vbQuestion + vbYesNo, "OPTION")
If ans = vbNo Then
Cancel = True
End If
End If
End Sub

Also, if I wanted to add a two carriage returns so that "Do you want to save the file anyway?" is on a separate line, how is that done?

Thank you.
 
Upvote 0
PosieQ:

You are getting the error because you have a quote mark after the word Subcontractor. Change that to '.

Also, if I wanted to add a two carriage returns so that "Do you want to save the file anyway?" is on a separate line, how is that done?

Code:
ans = MsgBox("A Prime's Prime Number is required when the role 'Subcontractor' is checked. If number was provided, please enter 'TBD' or 'N/A' as appropriate." _
& vbNewLine & vbNewLine & "Do you want to save the file anyway?", _
vbQuestion + vbYesNo, "OPTION")

Regards,

CJ
 
Upvote 0
PosieQ:

You are getting the error because you have a quote mark after the word Subcontractor. Change that to '.



Code:
ans = MsgBox("A Prime's Prime Number is required when the role 'Subcontractor' is checked. If number was provided, please enter 'TBD' or 'N/A' as appropriate." _
& vbNewLine & vbNewLine & "Do you want to save the file anyway?", _
vbQuestion + vbYesNo, "OPTION")

Regards,

CJ


CJ, thank you for catching the double quotes!

Your change works perfectly according to the conditions I provided, but I see now that I needed to provide more specifics. I thought that by clicking ‘No’ the user would be returned to the document, but instead the document completely closes. Can the code be changed so that the user is returned to the document when ‘No’ is clicked?

Also, rethinking this a little differently now, can the ‘Yes’ button be changed to ‘Continue Saving’ and ‘No’ changed to ‘Return to Document’?

I appreciate all your help!
 
Upvote 0
To prevent closing use the following code which used the BeforeClose event instead of the BeforeSave event:

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim ans As Variant
    If ActiveSheet.Range("A1").Value = True And ActiveSheet.Range("B2") = "" Then
        ans = MsgBox("A Prime's Prime Number is required when the role 'Subcontractor' is checked. If number was provided, please enter 'TBD' or 'N/A' as appropriate." _
        & vbNewLine & vbNewLine & "Do you want to close the file anyway?", _
        vbQuestion + vbYesNo, "OPTION")
            If ans = vbNo Then
                Cancel = True
            End If
    End If
End Sub


Also, rethinking this a little differently now, can the ‘Yes’ button be changed to ‘Continue Saving’ and ‘No’ changed to ‘Return to Document’?

Not with the standard msgbox, but you can create a userform with your message and commandbuttons with those captions.

CJ
 
Upvote 0

Forum statistics

Threads
1,214,989
Messages
6,122,622
Members
449,093
Latest member
catterz66

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