查找多列的匹配单元格值并发送有关匹配信息的邮件

时间:2019-02-10 12:54:28

标签: excel vbscript

我有一个Excel文件,其中包含以下不同的列(名称,角色,电子邮件)。我想搜索重复多次的角色(从“角色”列),并显示相应的值(从“名称”列)。最后,应该将有关角色信息的邮件发送到相应的邮件ID(在“电子邮件”列中提到)。同样需要VBScript方法。

Name     Role    Email
Ram(z123)MC_PLM_Team Architect (TA), MC_PLM_Software Developer Sh.ram@abc.com
Shm(Z003)MC_PLM_Chief Product Owner (CPO)       Gh.sham@xyz.com
Arn(z789)MC_SW Developer/Designer ES/HMI/NET       Kh.arun@wxy.com
Ach(z234)MC_PLM_Team Architect (TA), MC_PLM_Software Developer   Ch.Achal@klm.com

1 个答案:

答案 0 :(得分:0)

感谢您的投入。是的,我花了一些时间快速了解VB内部。编写了满足我要求的脚本。这是我的第一个脚本版本,将对其进行进一步的增强。

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

    intRows = Application.ActiveSheet.UsedRange.Rows.Count
    intCols = Application.ActiveSheet.UsedRange.Columns.Count

    For i = 12 To intRows
     resName = Application.ActiveSheet.Cells(i, 3).Value
     primRole = Application.ActiveSheet.Cells(i, 31).Value
     emailID = Application.ActiveSheet.Cells(i, 49).Value

     If resName <> "" Then
        MsgBox resName & vbNewLine & primRole & vbNewLine & emailID
     End If

    Next

    Set outApp = CreateObject("Outlook.Application")
    Set outMail = outApp.CreateItem(0)

    strBody = "Dear XYZ" & vbNewLine & "email from outlook" & vbNewLine & "Best Regards," & vbNewLine & "XYZ"
'   
    outMail.To = "sh.ram@abc.com"
    outMail.CC = ""
    outMail.BCC = ""
    outMail.Subject = "Subject Test VBA"
    outMail.Body = strBody
    outMail.Send

    Set outMail = Nothing
    Set outApp = Nothing


End Sub