在Excel中使用电子邮件地址的过滤列创建新电子邮件

时间:2019-02-11 18:45:35

标签: excel vba

在Excel VBA 2013中,要使用excel中经过筛选的单元格的选择作为电子邮件地址的源,以将其传递给生成新电子邮件的过程。

我的数据如下:

enter image description here

我有一行代码应该选择标题行之后的B列中的所有数据(电子邮件地址):

Dim recipients As String
....
recipients = Join(Application.Transpose(.Range("B6" & .Rows.Count).Value), ";")

此行会产生错误的无效或不合格的引用。

稍后将收件人字符串传递给电子邮件过程。为了完整起见,电子邮件VBA为:

With OutMail
  .Subject = "Some subject"
  .Body = "Get a body for the email"
  .To = recipients
  .Importance = olImportanceHigh
  .Display
End With

2 个答案:

答案 0 :(得分:1)

这是在代码中迭代过滤单元格的方法:

Dim recipients As String
Dim recipient As Range
With ThisWorkbook.Sheets("Sheet1") ' Or ActiveSheet
   For Each recipient In .Range("B6:B" & .UsedRange.Rows.Count).SpecialCells(xlCellTypeVisible).Cells
      recipients = recipients + recipient + ";"
   Next recipient
End With

答案 1 :(得分:0)

1:您的范围必须为:Range("B6:B" & .Rows.Count)

2:您应将行限制为已使用区域Range("B6:B" & .UsedRange.Rows.Count)

3:要仅选择过滤的单元格,您需要添加.SpecialCells(xlCellTypeVisible)

类似这样的东西:

recipients = Join(Application.Transpose(.Range("B6:B" & .UsedRange.Rows.Count).SpecialCells(xlCellTypeVisible).Value), ";")

4:最好迭代代码中的单元格,而不要使用转置/联接:否则,当仅过滤1个电子邮件地址或看不到电子邮件时,您可能会收到错误消息。

相关问题