VBA Excel - 如果一列中的单元格包含电子邮件地址,而另一列中的单元格为“已售出”,则发送电子邮件

时间:2013-09-12 20:26:11

标签: excel vba email excel-vba

刚开始尝试使用VBA。创建Excel工作表以跟踪SOLD,PENDING,LOST,这将允许销售人员单击按钮一次将组电子邮件发送到一个类别。经过多次搜索后,我找到了一些适用于通过检查列来发送组电子邮件的代码,以确保有适当的地址。我发现了一些其他代码,我认为会检查“作业状态”列,以便只有“已售出”或其他任何可用于电子邮件的代码。我是一个无知的初学者,需要帮助。这是代码,直到我添加 - 如果Sh.Cells(Target.Row,7)=“PENDING”然后 - 部分。任何帮助将不胜感激。谢谢!

Private Sub CommandButton1_Click()
Dim cell As Range
 Dim strto As String
 For Each cell In ThisWorkbook.Sheets("Sales 2013").Range("E3:E500")
 If cell.Value Like "?*@?*.?*" Then
    If Sh.Cells(Target.Row, 7) = "PENDING" Then
     strto = strto & cell.Value & ";"
    End If
 End If
 Next cell
 If Len(strto) > 0 Then strto = Left(strto, Len(strto) - 1)

Application.ScreenUpdating = False
 Set OutApp = CreateObject("Outlook.Application")
 OutApp.Session.Logon

 On Error GoTo cleanup
 Set OutMail = OutApp.CreateItem(0)
 On Error Resume Next
 With OutMail
 .To = "Anchor Sales"
 .Bcc = strto
 .Subject = "Enter subject here"
 .Body = "" ' EMPTY FOR NOW
 'USE THIS FOR ENTERING NAMES OF RECIPIENTS IN BODY TEXT "here"
 '"Dear" & Cells(cell.Row, "A").Value _
 & vbNewLine & vbNewLine & _
 "Enter body text " & _
 "here"
 'You can add files also like this
 '.Attachments.Add ("C:\test.txt")
 '.Send 'Or use Display
 .Display
 End With
 On Error GoTo 0
 Set OutMail = Nothing
cleanup:
 Set OutApp = Nothing
 Application.ScreenUpdating = True
End Sub

Private Sub CommandButton2_Click()

End Sub

请帮忙!

1 个答案:

答案 0 :(得分:0)

当然我更愿意每人发一封邮件(如果收件人不认识对方),但让我们继续解决您的问题。

你只需要做一些小改动:

Private Sub CommandButton1_Click()
Dim cell As Range
 Dim strto As String
 For Each cell In ThisWorkbook.Sheets("Sales 2013").Range("E3:E500")
 If cell.Value = "PENDING" Then strto = strto & cell.offset(0, 1).value & ";" 

我不确定你是否需要“If if cell.Value Like”部分? @?。?*“”或者如果是你发现的代码中的复制粘贴...如果需要,最后一行必须由

替换
 If cell.Value Like "?*@?*.?*" and cell.Value = "PENDING" Then strto = strto & cell.offset(0, 1).value & ";"  

    'in Offset(0,1) I assume the mailadress is in the cell next to the cell tested for "pending" 
 Next cell
 If Len(strto) > 0 Then strto = Left(strto, Len(strto) - 1)

其余的就像你拥有它一样。

问题是由

引起的
    If Sh.Cells(Target.Row, 7) = "PENDING" Then

因为你没有“sh”的定义 - 但你也不需要它。

我希望这会有所帮助