如果单元格的值为1,则将电子邮件地址添加到Outlook电子邮件中

我试图得到一些帮助,发送一封电子邮件给E列中的“1”的任何人。我有一个员工和他们的电话号码范围的工作表。 在他们的电话号码之后是他们想要被通知的事件的列表。 如果我在他们的名字后面加一个1,我想他们的电子邮件地址添加到电子邮件中。

列标题将像A =姓,B =名,C =电话号码,D =电子邮件地址,E =事件1,F =事件2,G =事件3等等。

我能够得到电子邮件的代码来从一个设置的范围内拉取所有的电子邮件地址,但我不知道如何添加if命令来查找设置值的事件列之一(E,F,G .. )。 它将需要循环遍历整个范围,然后仅为那些在某个事件列中具有设置值的所选个人提取地址。

这是我的代码到目前为止:

Dim OutApp As Object Dim OutMail As Object Dim strto As String, strcc As String, strbcc As String Dim strsub As String, strbody As String Dim emailRng As Range, cl As Range Dim sTo As String Set emailRng = Worksheets("Sheet1").Range("D1:D500") For Each cl In emailRng sTo = sTo & ";" & cl.Value Next sTo = Mid(sTo, 2) Set OutApp = CreateObject("Outlook.Application") Set OutMail = OutApp.CreateItem(0) strto = sTo strcc = "" strbcc = "" strsub = "'NOTIFICATION' - " & "Notification" strbody = "<img src=Z:\Logo2.jpg width=624 height=74>" & _ "<font size=2 font face=Verdana color=black>" & "<br>" & _ "The following notification was received:</B><br><br>" & _ "COPY AND PAST NOTIFICATION RECEIVED HERE" & "<br><br>" & _ "<b>Control Center</b><br><br>" With OutMail '.SentOnBehalfOfName = "" .to = strto .cc = strcc .bcc = strbcc .Subject = strsub 'You can add a file to the mail like this .HTMLBody = strbody .Display ' or use .Send End With Set OutMail = Nothing Set OutApp = Nothing 

基本的答案是这样的:

 For Each cl In emailRng If cl.Offset(,1) = 1 Then sTo = sTo & ";" & cl.Value Next 

在这里,您检查是否检查电子邮件地址(列E中的事件1)的1列是否被通知。

然而,既然你说过你可以有n个事件,你将需要一种方法来确定你正在发送一个电子邮件的事件。 我不知道你打算如何处理,但你可以,例如,从另一个Sub调用Sub并传递事件号码,并将其用作偏移量。

所以

 Sub EmailEvent() SendEventEmail 1 'send event 1 email SendEventEmail 2 'send event 2 email SendEventEmail 3 'send event 3 email End Sub 

然后在你的sub中,实际上发送它会说的邮件

 Sub SendEventEmail(n as Long) Rem n is the Event Number Rem Your Code ... For Each cl in emailRng If cl.Offset(,n) = 1 Then sTo = sTo & ";" & cl.Value Next Rem Rest of code ... End Sub