VBA 按列中的通用值对表数据进行分组,用于 Outlook 消息

问题描述 投票:0回答:1

我有一张员工数据表,每行都包含其经理的电子邮件地址。我正在编写 VBA,以将 Outlook 消息发送到名为“经理电子邮件”的表列中的电子邮件地址。我希望电子邮件包含该行中其他几个单元格的数据。诀窍是,我想将具有同一经理的员工的数据分组到一封电子邮件中,可能在邮件正文中用换行符分隔。

我认为这与在电子邮件列中搜索匹配的电子邮件地址有关,但我是一个 VBA 新手,不确定从哪里开始。我的电子邮件起草代码工作正常,但每行都有一条单独的消息。到目前为止就是这样:

Public Sub Email()
Dim objOutlook As Outlook.Application
Dim objMail As Outlook.MailItem
Dim rng As Range
Set rng = Sheets("Audit").ListObjects("Table1").ListColumns("Manager Email").DataBodyRange
Set objOutlook = Outlook.Application
For Each cell In rng

    Set objMail = objOutlook.CreateItem(olMailItem)

    objMail.To = Range(cell.Address).Value
    
    objMail.Subject = "(Redacted)"
    
    'for testing, I am just pulling one cell from the row of the address being emailed, two columns away. If there is a way to be specific about the column rather than use Offset, that would be preferable
  
    objMail.Body = Range(cell.Address).Offset(0, 2).Value
  
    objMail.Close (olSave)
    Set objMail = Nothing 
    Next 
    End Sub

感谢您的帮助!

excel vba for-loop outlook grouping
1个回答
0
投票

无需“搜索”匹配地址,只需对该列进行排序即可。从数据顶部开始,将“检查”变量设置为空白。在每一行上根据当前行的电子邮件地址检查该变量 - 如果不同,则需要发送电子邮件。如果相同,则只需在当前电子邮件中添加另一行数据即可。在每一行之后,将检查变量设置为该行中的电子邮件,然后继续到下一行。

编辑 - 意思是说,如果您不确定 VBA 对列进行排序,则使用“开发人员”、“记录宏”。生成的代码需要整理,但会让您走上正确的道路

© www.soinside.com 2019 - 2024. All rights reserved.