VB Code - Validate/Verify Conditions are met before allowing Emails to be sent

wcm69

Board Regular
Joined
Dec 25, 2016
Messages
112
Hoping a smarter mind than mine can help out<o:p></o:p>
In my spreadsheet I have a dropdown list (Cell I12) with 2options: “Deliver to:” and “Collection”.<o:p></o:p>
I would like a VB Code to validate/verify a number of conditions/criteriabefore allowing an email order to be sent. <o:p></o:p>
The conditions/criteria that need to be met before allowingan email to be sent are:<o:p></o:p>
1) The (Dropbox) option: “Deliver to:” chosen and the address fields below (CellsI13:I17) are filled in.
Or if:

2) The (Dropbox) option: “Collection” chosen and the address fields below (Cells I13:I17) are not filled in / empty.<o:p></o:p>
Otherwise - the code should NOT allow an email to besent and exit the sub with a MsgBox -something like.[“Complete address fields”]. It would be great if the cursor goesback to the first blank/empty cell (but not important).<o:p></o:p>
I’m using the code below with varying results but haven’t beenable to get what I’m really after as. Unfortunately I’m still picking up VBA. Any help asusual is appreciated.

code:

Sub Emailit()<o:p></o:p>
IfRange("N13").Value = "" Then<o:p></o:p>
MsgBox "You havenot added a date"<o:p></o:p>
Exit Sub<o:p></o:p>
End If<o:p></o:p>
IfRange("G19").Value = "" Or Range("H19").Value ="" Or Range("I19").Value = "" Then<o:p></o:p>
MsgBox "Have youhave not added your order"<o:p></o:p>
Exit Sub<o:p></o:p>
End If<o:p></o:p>
Application.Dialogs(xlDialogSendMail).Show"someone@somewhere.com"<o:p></o:p>
Clear_Order<o:p></o:p>
End Sub<o:p></o:p>
Many thanks in advance :)<o:p></o:p>
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
1) The (Dropbox) option: “Deliver to:” chosen and the address fields below (CellsI13:I17) are filled in.
Or if:

2) The (Dropbox) option: “Collection” chosen and the address fields below (Cells I13:I17) are notfilled in / empty.


Please clarify/provide some amplifying information:

1. If Deliver to is the option selected...then all cells in I13:I17 need to be filled in? Or do you want to check if at least one cell in I13:I17 has data in it?

2. If Collection is the option selected...then all cells in I13:I17 need to be blank?
 
Upvote 0
Here's what I came up with:

Code:
Sub Emailit()
If Range("N13").Value = "" Then
    MsgBox "You have not added a date."
    GoTo exitHandler
End If

If Range("G19").Value = "" Or Range("H19").Value = "" Or Range("I19").Value = "" Then
    MsgBox "You have not added your order."
    GoTo exitHandler
End If

If Range("I12").Value = "Deliver to:" Then
    If Not IsEmpty(Range("I13")) And Not IsEmpty(Range("I14")) And Not IsEmpty(Range("I15")) And Not IsEmpty(Range("I16")) And Not IsEmpty(Range("I17")) Then
        GoTo sendEmail
    Else
        MsgBox "You need to fill in the address fields."
        GoTo exitHandler
    End If
ElseIf Range("I12").Value = "Collection" Then
    If IsEmpty(Range("I13")) And IsEmpty(Range("I14")) And IsEmpty(Range("I15")) And IsEmpty(Range("I16")) And IsEmpty(Range("I17")) Then
        GoTo sendEmail
    Else
        MsgBox "You have data in the address fields. Please remove this data before continuing."
        GoTo exitHandler
    End If
ElseIf Range("I12").Value = "" Then
    MsgBox "Please make a selection in Cell I12 and proceed accordingly."
    GoTo exitHandler
End If

exitHandler:
Exit Sub

sendEmail:
'code to send email
Application.Dialogs(xlDialogSendMail).Show "someone@somewhere.com"
Clear_Order
Resume exitHandler
End Sub

Specifically regarding the code I added, the only issue I see you encountering is if any of the cells appear to be blank but are not actually blank (i.e. a space entered into the cell), then you could get some unintended results.
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,071
Latest member
cdnMech

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