Only run macro if the first column contains text - VBA Excel to Outlook

VBAn00bheh

New Member
Joined
Jul 8, 2019
Messages
1
Hi All,

This is my current code:
Code:
'binding


Sub bulk_email()
On Error Resume Next


Dim o As Outlook.Application
Set o = New Outlook.Application
Dim omail As Outlook.MailItem
Dim Signature As String


Dim i As Long
For i = 2 To Range("A10000").End(xlUp).Row
Set omail = o.CreateItem(olMailItem)
    
emailString = "<font size=""2"" face=""Verdana"" color=""black"">" & Cells(i, 5).Value & "<br>" & "<br>" _
    & Cells(i, 6).Value & "<br>" & "<br>" _
    & Cells(i, 7).Value & "<br>" & "<br>" _
    & Cells(i, 8).Value & "<br>" & Cells(i, 9).Value & "</font>"
    
With omail


    .Display
    .Sender = Cells(i, 1).Value
    .To = Cells(i, 2).Value
    .CC = Cells(i, 3).Value
    .Subject = Cells(i, 4).Value
    .HTMLBody = emailString & .HTMLBody
    
    .Attachments.Add Cells(i, 10).Value
    .Attachments.Add Cells(i, 11).Value
    .Attachments.Add Cells(i, 12).Value
    .Attachments.Add Cells(i, 13).Value
    .Attachments.Add Cells(i, 14).Value
    .Attachments.Add Cells(i, 15).Value
    
    .Display
    
End With
Next


End Sub

Each row is a separate email with different recipients and attachments. If I run the macro, it runs everything. How do I make it run ONLY IF Column A contains my own email address? Like if it's blank, the window for that shouldn't appear in Outlook.


Thank you so much! I appreciate your help. :)
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
If you want VBA to
- check the value in column A for each iteration
- and only generate an email if that value matches your email address
- then try with these 3 modifications (UNTESTED)

Code:
Sub bulk_email()
On Error Resume Next

Dim o As Outlook.Application
Set o = New Outlook.Application
Dim omail As Outlook.MailItem
Dim Signature As String
Dim i As Long
[COLOR=#006400]Const myEmail = "myName@gmail.com"[/COLOR]


For i = 2 To Range("A10000").End(xlUp).Row
[COLOR=#006400]    If Cells(i, 1) = myEmail Then[/COLOR]
        Set omail = o.CreateItem(olMailItem)
            
        emailString = "" & Cells(i, 5).Value & "" & "" _
            & Cells(i, 6).Value & "" & "" _
            & Cells(i, 7).Value & "" & "" _
            & Cells(i, 8).Value & "" & Cells(i, 9).Value & ""
            
        With omail
             '.......etc
        
        End With
[COLOR=#006400]    End If[/COLOR]
Next


End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

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