VBA Vlookup发送不匹配特定电子邮件的电子邮件

时间:2019-04-17 14:48:21

标签: vba match vlookup offset

我正在尝试Vlookup特定单元格以返回联系人列表中的匹配项。当找到匹配项时,它将向与该位置相关联的特定人员发送电子邮件。 vlookup可以工作,而电子邮件也可以工作,但是在发送电子邮件时,该电子邮件将填充名称列表中的所有名称,不仅填充与该位置关联的特定电子邮件或正在查找的单元格。

Sub vLookupAnotherWorksheet()
Dim myLookupValue As String
Dim myFirstColumn As Long
Dim myLastColumn As Long
Dim myColumnIndex As Long
Dim myFirstRow As Long
Dim myLastRow As Long
Dim myVLookupResult As Long
Dim myTableArray As Range

myLookupValue = "H3:H13"
myFirstColumn = 1
myLastColumn = 8
myColumnIndex = 8
myFirstRow = 3
myLastRow = 13

With Worksheets("EVC_Contact_List")
Set myTableArray = .Range(.Cells(myFirstRow, myFirstColumn), .Cells(myLastRow, myLastColumn))
End With

On Error Resume Next
myVLookupResult = WorksheetFunction.VLookup(myLookupValue, myTableArray, myColumnIndex, False)

If IsError(myVLookupResult) = False Then
Call Send_Email(myvalue)

End If


End Sub

Sub Send_Email(myvalue As Variant)


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, namelist2 As String, o As Variant

    Email_Send_From = ""

    If Sheets("EVC_Contact_List").Cells(2, 4).Value <> "" Then
        nameList = Join(Application.WorksheetFunction.Transpose(Sheets("EVC_Contact_List").Range("D2:D29")))
        namelist2 = Join(Application.WorksheetFunction.Transpose(Sheets("EVC_Contact_List").Range("F2:F29")))
    End If


    Set Mail_Object = CreateObject("Outlook.Application")
    With Mail_Object.CreateItem(o)
        .Subject = "Unit(s) Excceding Days as Loaner"
        .To = nameList
        .Cc = namelist2

        .display
    End With
        Application.DisplayAlerts = False

End Sub

因此,如果在联系人列表中找到位置XXXX,并且Johnsmith@gmail.com与该位置相关联,它将仅向John Smith发送电子邮件。在这种情况下,我的代码正在向联系人列表中的每个人发送电子邮件。

1 个答案:

答案 0 :(得分:0)

你有这个:

nameList = Join(Application.WorksheetFunction.Transpose(Sheets("EVC_Contact_List").Range("D2:D29")))

那不是Vlookup函数吗?