VBA Code to Send Mail to skip blank cells

Ben AFF

Board Regular
Joined
Sep 21, 2023
Messages
55
Office Version
  1. 365
Platform
  1. Windows
Hi All,

Appreciate if you can help me with the following.

I have a macro code to send emails to all selected rows in columns E&F of an spreadsheet.
When any rows of E&F columns are selected the macro will look in column M & N for the .To and .CC addresses and generate an email.
This part is working well.
The problem is that the macro will generate an error if there is no mail address or blank value in column M.
I want to modify the code so when there is no email address the code stops trying to send a mail and generate an error.
Please can you help me? Thank you.

I past below the macro code.

VBA Code:
Sub ExcelToOutlookSR()
Dim mApp As Object
Dim mMail As Object
Dim SendToMail As String
Dim MailSubject As String
Dim mMailBody As String
Dim r As Range, n As Long
Dim d As Object, f As Object
Dim tx As String
Dim x, ary, g

UserResponse = MsgBox("This will send e-mails for selected cells, are you sure?", vbYesNo + vbExclamation, "Send Mail?")
  
'If No exit sub
   If UserResponse = vbNo Then
    Exit Sub
   End If

If Not Intersect(Selection, Range("E:F")) Is Nothing And Intersect(Selection, Range("G:XFD")) Is Nothing Then

    n = Range("E" & Rows.Count).End(xlUp).Row 'get last row with data in col E
    If Not Intersect(Selection, Range("E2:E" & n)) Is Nothing Then
        Set d = CreateObject("scripting.dictionary"):        d.CompareMode = vbTextCompare
        Set f = CreateObject("scripting.dictionary"):        f.CompareMode = vbTextCompare
        
        For Each r In Intersect(Selection, Range("E2:E" & n))
            f(r.Value) = Empty
        Next
        
        For Each r In Range("E2:E" & n)
            tx = r.Value
            If f.Exists(tx) Then
                If Not d.Exists(tx) Then
                    d(tx) = r.Row
                Else
                    d(tx) = d(tx) & " " & r.Row
                End If
            End If
        Next
       
        For Each x In d
            ary = Split(d.Item(x), " ")
            SendToMail = Range("M" & ary(0))
            CopyToMail = Range("N" & ary(0))
            MailSubject = Range("K" & ary(0))
            tx = ""
            For Each g In ary
                tx = tx & vbLf & Range("L" & g)
            Next
            mMailBody = Mid(tx, 2)
'            Debug.Print mMailBody
            Set mApp = CreateObject("Outlook.Application")
            Set mMail = mApp.CreateItem(0)
            With mMail
                .To = SendToMail
                .CC = CopyToMail
                .Subject = MailSubject
                .Body = "Dear " 
                .Send
                                  
            Selection.Copy
            ThisWorkbook.Worksheets("Log").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
            End With
        Next
        MsgBox "@mails are sent"
    Else
        MsgBox "Please, select cells with data in column E&F only."
    End If
Else
    MsgBox "Please, select cells with data in column E&F only."
End If

End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Should work if you validate SendToMail within an IF block? Maybe
VBA Code:
If SendToMail <> "" Then
    With mMail
        .To = SendToMail
        etc.
    End With
End If
If not there then not sure. That looks rather complicated for what it does.
 
Upvote 0

Forum statistics

Threads
1,224,742
Messages
6,180,685
Members
452,993
Latest member
FDARYABEE

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