VBA使用If语句和查找发送电子邮件

时间:2014-10-30 14:13:44

标签: email excel-vba vba excel

我正在尝试完成一个电子邮件代码,该代码会在触发列表时向收件人列表发送电子邮件。我在名为"电子邮件列表"的工作表的A列:K中列出了大约10个名字。

我有另一张名为" Info Table"的表格。此工作表包含一个表格,其中包含国家/地区所在的列。

我希望添加能够执行以下操作的代码:当某个国家/地区位于"信息表" (C列)与"电子邮件列表"中的国家/地区匹配。 (第2行,A列:K列),电子邮件将发送到该国家/地区第3-13行中列出的电子邮件。此阶段的电子邮件可以是通用的。

我非常感谢每个花时间了解并回应需求的人。我想在这里学习并取得成功,请知道每一个贡献都非常感谢。

Sub Send_Email()

Dim Email_Subject, Email_Send_From, Email_Body As String, i As Integer
Dim Mail_Object, nameList As String, o As Variant

    Email_Send_From = ""
    For i = 3 To 13 'use cells 3 to 13 in column "A" where names are stored

    If Sheets("Email List").Range("A3").Value <> "" Then
    nameList = nameList & ";" & Sheets("Email List").Range("A" & i).Value
    End If

    Next
    Set Mail_Object = CreateObject("Outlook.Application")
    With Mail_Object.CreateItem(o)
        .Subject = ""
        .To = nameList
        .Cc = ""
        .Body = "I am testing a new VBA, sorry if you received this message in error." & vbNewLine & vbNewLine & _
                "Best Regards," & vbNewLine & _
                ""
        .display
    End With
        Application.DisplayAlerts = False

End Sub

1 个答案:

答案 0 :(得分:1)

是的,您可以使用Application.VLookup函数,该函数采用与同名工作表函数相同的参数。

Dim myValue as Variant

'Modify the function to use YOUR arguments
myValue = Application.Vlookup(value_to_lookup, range_to_search, column_number, exact_match)

if IsError(myValue) Then 
    MsgBox "Not found!", vbInformation
    Exit Sub
End If

请说明这意味着什么,并且应该很容易帮助修改您的代码:

  

当某个国家/地区位于&#34;信息表&#34; (C列)与国家/地区相匹配   &#34;电子邮件列表&#34; (第2行,A列:K列),电子邮件将发送到电子邮件   在该国家/地区的第3-13行中列出。

具体来说:什么会触发Send_Email程序?你怎么称呼它?是否需要循环搜索信息表(C列)中的所有值?等


与此同时,让我们稍微改进您的代码。 VBA 支持隐式类型声明,每个变量必须为Dim,并且它自己为As {type},否则它将被视为Variant。最佳做法是尽可能避免变体支持强类型变量。

您还可以避免For i = 3 to 13循环并在单个语句中分配给nameList

然后,我只是在C列循环中调用此过程,使用Match函数检查电子邮件列表工作表中是否存在该国家/地区。

Sub foo()
Dim c as Range
Dim match as Integer

With Sheets("Info Table")
    For each c in .Range("C1:C10").Cells ''Modify this as needed
        match = 0
        On Error Resume Next
        match = IfError( _
                     Application.Match( _
                         c.Value, Sheets("Email List").Range("A2:K2"), False)
                       )
        On Error GoTo 0
        If Not match = 0 Then Call Send_Email(match)
    Next

End Sub

如果存在,则将匹配函数的结果发送到Send_Email过程,并使用match定义范围以指示包含电子邮件的该特定国家/地区的列表:

Sub Send_Email(match as Integer)

Dim Email_Subject As String, Email_Send_From  As String, Email_Body As String, i As Integer
Dim Mail_Object as Object, nameList As String, o As Variant

    Email_Send_From = ""

    If Sheets("Email List").Cells(match, 3).Value <> "" Then
        nameList = Join(Application.Transpose(Sheets("Email List").Range("A3:A13").Offset(,match-1).Value, ";")
    End If

    Next
    Set Mail_Object = CreateObject("Outlook.Application")
    With Mail_Object.CreateItem(o)
        .Subject = ""
        .To = nameList
        .Cc = ""
        .Body = "I am testing a new VBA, sorry if you received this message in error." & vbNewLine & vbNewLine & _
                "Best Regards," & vbNewLine & _
                ""
        .display
    End With
        Application.DisplayAlerts = False

End Sub